Php 使用cURL输出
我计划录制一个特定主题的实时推文。同样,我在PHP中使用带有cURL的twitter流API 代码如下:Php 使用cURL输出,php,curl,twitter-streaming-api,Php,Curl,Twitter Streaming Api,我计划录制一个特定主题的实时推文。同样,我在PHP中使用带有cURL的twitter流API 代码如下: <?php $username = "xxxxx"; $password = "xxxxx"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://stream.twitter.com/1/statuses/filter.json? track=SEARCH_PARAMETER'); curl_se
<?php
$username = "xxxxx";
$password = "xxxxx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://stream.twitter.com/1/statuses/filter.json? track=SEARCH_PARAMETER');
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$jsonOBJ = json_decode ($result);
curl_close($ch);
print_r($jsonOBJ);
?>
<?php
$query = "SEARCH_PARAMETER";
$request = "http://search.twitter.com/search.json?q=".urlencode($query);
$response = file_get_contents($request);
$jsonobj = json_decode($response);
print_r($jsonobj);
?>
我的问题是,如果我将CURLOPT_RETURNTRANSFER设置为0,我可以在终端上看到推文。但是我不能存储在变量$jsonOBJ中并打印它
请帮忙 我也在做同样的事情:)问题是,当你在终端中做这件事时,它是一个流,所以连接一直保持活动状态,直到你杀死它。(即curl_exec()未完成)
尝试查看CURLOPT_PROGRESSFUNCTION和CURLOPT_READFUNCTION。他们可能会给你一些提示。我只是赶着给你一个答案,因为我今天就要出发了。这看起来应该行得通 下面是我使用的一个函数,我向它传递一个URL和一些XML数据,它返回一个关联数组,表示成功为true或false,返回值为字符串
function do_curl($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec ($ch);
$curl_return=array();
if (!is_string($result))
{
$curl_return['STATUS'] = FALSE;
$curl_return['ERRMSG'] = curl_error($ch);
}
else
{
$curl_return['STATUS'] = TRUE;
$curl_return['RESPONSE'] = $result;
}
curl_close($ch);
return $curl_return;
}
更新:查看消息末尾的新代码,使用cURL实际上很容易做到这一点,但我第一次做得不正确 我无法将cURL与
CURLOPT_READFUNCTION
结合使用,但成功地使用了andfread()
。我不确定readfunction为什么不能像我以前成功使用过的那样工作,但它一定与响应数据是“流式”的,并且不是使用HTTP分块编码发送的这一事实有关。实际上,我的read函数从未被调用,因此我无法处理数据
我所使用的方法现在正在发挥作用:
- 使用fsockopen连接到
ssl://stream.twitter.com
- 使用
fputs
- 使用HTTP响应头并确保没有错误
- 在无限循环中使用
读取数量数据fread
- 每次读取数据块时,我调用一个内部缓冲函数
- 缓冲区函数将新数据附加到缓冲区
- 缓冲区函数然后尝试处理缓冲区中的所有消息(如果我们有一条或多条完整消息)
- 当它处理每条消息时,缓冲区将被减少,直到其为空,然后函数返回并再次读取数据
<?php
$USERNAME = 'youruser';
$PASSWORD = 'yourpass';
$QUERY = 'nike';
/**
* Called every time a chunk of data is read, this will be a json encoded message
*
* @param resource $handle The curl handle
* @param string $data The data chunk (json message)
*/
function writeCallback($handle, $data)
{
/*
echo "-----------------------------------------------------------\n";
echo $data;
echo "-----------------------------------------------------------\n";
*/
$json = json_decode($data);
if (isset($json->user) && isset($json->text)) {
echo "@{$json->user->screen_name}: {$json->text}\n\n";
}
return strlen($data);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://stream.twitter.com/1/statuses/filter.json?track=' . urlencode($QUERY));
curl_setopt($ch, CURLOPT_USERPWD, "$USERNAME:$PASSWORD");
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'writeCallback');
curl_setopt($ch, CURLOPT_TIMEOUT, 20); // disconnect after 20 seconds for testing
curl_setopt($ch, CURLOPT_VERBOSE, 1); // debugging
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); // req'd to get gzip
curl_setopt($ch, CURLOPT_USERAGENT, 'tstreamer/1.0'); // req'd to get gzip
curl_exec($ch); // commence streaming
$info = curl_getinfo($ch);
var_dump($info);
@Reza Sanaie和其他可能认为这有帮助的人
我使用了搜索TWITTER API,并获得了实时推文。所以这可能会有帮助。
代码如下:
<?php
$username = "xxxxx";
$password = "xxxxx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://stream.twitter.com/1/statuses/filter.json? track=SEARCH_PARAMETER');
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$jsonOBJ = json_decode ($result);
curl_close($ch);
print_r($jsonOBJ);
?>
<?php
$query = "SEARCH_PARAMETER";
$request = "http://search.twitter.com/search.json?q=".urlencode($query);
$response = file_get_contents($request);
$jsonobj = json_decode($response);
print_r($jsonobj);
?>
我还设置了MySQL连接以将其推送到数据库中,这个脚本被添加到crontab中以自动化整个过程。使用sdk for php和oauth会不会更容易?打印$result以查看它在尝试解码之前的样子。现在,我直接做这个“echo$result;”。但在执行此脚本时没有显示任何内容。您正在尝试解码可能达到10000或超过125MB数据的结果。。。。您应该使用流
而不是curl@Baba我添加了“curl_setopt($ch,CURLOPT_TIMEOUT,30);”,这样我可以获得有限的数据量并终止curl操作。但变量中仍然没有存储任何数据。谢谢你的建议。我将研究“stream”,我添加了“curl_setopt($ch,CURLOPT_TIMEOUT,30);”来杀死curl_exec()。但是变量中仍然没有存储任何数据。在这30秒内是否有tweets?是的。我可以通过打印到终端上看到它们。老实说,我正试图解决同样的问题。问题是我们不能每30秒连接一次twitter,他们会阻止我们。只要我们打算读取流,就需要有一个对他们开放的连接。因此,它可能必须在后台运行,将其写入另一个流,然后网页读取第二个流。。。。或者有一个更简单的方法我没想到。非常感谢你的建议。现在,我找到了一个使用搜索twitter API的解决方案。当我再次使用流式API时,我一定会与您联系。@Rubie_Newbie我添加了一个使用cURL的流式API的工作示例。我还有一个C语言的工作版本。这个php类可能也会引起您的兴趣,尽管它不支持gzip。