使用fsockopen重写一些代码以改为使用curl

时间:2011-01-30 21:07:27

标签: php curl fsockopen fclose fputs

我的主机不允许fsockopen,但它确实允许卷曲。我很高兴使用cli中的curl,但不必将它与PHP一起使用。我怎么用curl写这个呢?

$ xmlrpcReq和Length是先前定义的。

$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);
}

非常感谢!

1 个答案:

答案 0 :(得分:2)

试试这个:

$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);