我想知道是否有一种方法可以通过已经打开的套接字使用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/