重写使用fsockopen使用卷曲,而不是
问题描述:
我的主机不允许的fsockopen一些代码,但它允许卷曲。我很高兴使用cli的curl,但没有必要在PHP中使用它。我该如何使用curl来编写它?重写使用fsockopen使用卷曲,而不是
$ xmlrpcReq和长度早先定义。
$host = "http://rpc.pingomatic.com/";
$path = "";
$httpReq = "POST /" . $path . " HTTP/1.0\r\n";
$httpReq .= "User-Agent: My Lovely CMS\r\n";
$httpReq .= "Host: " . $host . "\r\n";
$httpReq .= "Content-Type: text/xml\r\n";
$httpReq .= "Content-length: $xmlrpcLength\r\n\r\n";
$httpReq .= "$xmlrpcReq\r\n";
if ($pinghandle = fsockopen($host, 80)) {
fputs($pinghandle, $httpReq);
while (!feof($pinghandle)) {
$pingresponse = fgets($pinghandle, 128);
}
fclose($pinghandle);
}
非常感谢!
答
试试这个:
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'My Lovely CMS');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlrpcReq);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curl, CURLOPT_URL, "http://rpc.pingomatic.com/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$pingresponse = curl_exec($curl);
你怎么知道你的主机不允许的fsockopen?托管服务器这种做法并不常见,因为它会禁用所有漂亮的http库(=不卷曲)。你收到哪条错误消息? (否则,我会认为错误来自无效的主机:头。) – mario 2011-01-30 21:15:31
是的,我已经确认与主机,fsockopen是不允许的。垃圾! – 2011-01-30 21:22:32