我的主机不允许 fsockopen,但允许 curl。我很高兴从 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);
}
非常感谢!
最佳答案
试试这个:
$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);
关于php - 使用 fsockopen 重写一些代码以使用 curl 代替,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4845542/