Reescritura de código usando fsockopen para usar curl en su lugar
Pregunta
Mi host no permite fsockopen, pero sí permite curl.Estoy feliz de usar curl de la cli, pero no he tenido que usarlo mucho con PHP.¿Cómo escribo esto usando curl en su lugar?
$ xmlrpcReq y Length se definieron anteriormente.
$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);
}
¡Muchas gracias!
Solución
Pruebe esto:
$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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow