Rich Bradshaw
Rich Bradshaw

Reputation: 73055

Rewriting some code using fsockopen to use curl instead

My host doesn't allow fsockopen, but it does allow curl. I'm happy using curl from 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!

Upvotes: 3

Views: 1436

Answers (1)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99929

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

Upvotes: 2

Related Questions