php - 使用 fsockopen 重写一些代码以使用 curl 代替

标签 php curl fsockopen fclose fputs

我的主机不允许 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/

相关文章:

php - Laravel Livewire : load livewire component with button click

javascript - 同时运行 cURL 和 Node 服务器

php - fsockopen 通常很慢吗?

php - 如果我想用撇号的文本更新文本区域字段,htmlentities 是否会影响 htmlspecialchars?

php - 对 AJAX 的基础知识感到困惑

php - 需要 $.ajax 函数的帮助 - 如何发送关联数组?

curl - SonarQube 5.6 恢复质量配置文件

javascript - 如何将javascript请求转换为curl

java - 将字符写入 Java 套接字时 fsockopen 10053 错误

php - 具有 fsockopen 的 PayPal IPN 返回 NULL