Riscrivere del codice usando fsockopen per usare invece curl
Domanda
Il mio host non consente fsockopen, ma consente curl.Sono contento di usare curl dal cli, ma non ho dovuto usarlo molto con PHP.Come scrivo usando invece curl?
$ xmlrpcReq e Length sono stati definiti in precedenza.
$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);
}
Grazie mille!
Soluzione
Prova questo:
$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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow