php - curl 穿过已经打开的套接字

标签 php sockets curl

我想知道是否有一种方法可以通过已经打开的套接字使用curl,例如调整此:

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

使用curl_exec()代替fgets($fp, 128)

(或者,始终在同一个流上使用 curl() 的任何其他方式,我的目标是读取 twitter 流 api)

谢谢

最佳答案

这可能对你有用,因为你正在处理 Twitter Stream API

set_time_limit(0);
$ch = curl_init();
echo "<pre>";
curl_setopt($ch, CURLOPT_URL, "https://sitestream.twitter.com/2b/site.json");
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'cgets');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_USERPWD, 'user:password');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000000);
curl_exec($ch);

function cgets($ch, $string) {
    $length = strlen($string);
    printf("Received %d byte\n", $length);
    flush();
    return $length;
}

关于php - curl 穿过已经打开的套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12913777/

相关文章:

PHP 检查静态变量是否已声明或初始化?

PHP - 当给定子键名称时,递归地将每个数组元素的键设置为子元素的值

php - 与 PHP 集成的交易 express cURL 不起作用?

C++ Unix 域套接字工具包

php - PHP 中的 undefined variable

json - 将外部数据文件上传到 Apache Solr 的方法

php - Codeigniter更新SQL问题

php - 如何在 PHP 中动态定义一个变量

c++ - 你如何循环读取 C++ 套接字?

iphone - 如何使iPhone使用套接字与服务器通信?