开发者

Rewriting some code using fsockopen to use curl instead

开发者 https://www.devze.com 2023-02-07 19:21 出处:网络
My host doesn\'t allow fsockopen, but it does allow curl. I\'m happy using curl fro开发者_C百科m the cli, but haven\'t had to use it with PHP much. How do I write this using curl instead?

My host doesn't allow fsockopen, but it does allow curl. I'm happy using curl fro开发者_C百科m the cli, but haven't had to use it with PHP much. How do I write this using curl instead?

$xmlrpcReq and Length are defined earlier.

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

Thanks a lot!


Try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消