我正在努力使下面的代码与curl或其他东西一起工作。我已经看过卷曲fsockopen conversion,但它不起作用。代码工作,但在我的主机上禁用fsockopen。任何帮助将不胜感激。
$host = substr($hostport,0,50);
$port = substr($hostport,50,10);
$issecure = substr($hostport,60,1);
if($issecure=="Y")
{
$host = "ssl://".$host;
}
$fsok = fsockopen(trim($host) , intval(trim($port)));
if(FALSE == $fsok )
{
echo "Target Host not Found/Down"; return ;
}
fwrite($fsok, $bodyData );
$port ='';$host ='';$hostport= '';$bodyData='';
while ($line = fread($fsok, 25000))
{
echo $line;
}
fclose($fsok);
return $line;
答案 0 :(得分:1)
假设你使用http / https作为协议,那么以下内容应该有效:
$client = curl_init();
$options = array(
CURLOPT_PORT => $port
CURLOPT_RENTURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false, // dont verify the cert
CURLOPT_URL => ($issecure ? 'https://' : 'http://'). $host
);
$response = curl_exec($client);
if(false !== $response) {
echo $response;
return $response;
} else {
if($error = curl_error($client)) {
echo $error;
return $error;
} else {
echo 'Something is wrong. Error could not be determined.';
}
}
使用选项的另一个优秀和实用的选项是Zend_Http_Client
它支持fsockopen
和curl
,因此您可以轻松地来回切换而无需修改代码。