Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP获取json格式的最新推文_Php_Twitter - Fatal编程技术网

PHP获取json格式的最新推文

PHP获取json格式的最新推文,php,twitter,Php,Twitter,我正在使用以下代码解析来自twitter帐户的最新推文: $host = "http://search.twitter.com"; $filename = "/search.json"; $opts = array('http' => array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" )); $context = stream_cont

我正在使用以下代码解析来自twitter帐户的最新推文:

    $host = "http://search.twitter.com";
    $filename = "/search.json";

    $opts = array('http' => array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n"
    ));
    $context = stream_context_create($opts);

    $search = "mashable";
    $search = str_replace(" ", "%20", $search);
    $count = "10";

    $a = "$host$filename?q=$search&rpp=$count&include_entities=true";
    echo "$a\n";
    $json = file_get_contents($a, false, $context);

    $obj = json_decode($json, true);
    $id = $obj['results'][0]['id'];
    $tweet = $obj['results'][0]['text'];
    $user = $obj['results'][0]['from_user'];
    $to_user = $obj['results'][0]['to_user'];
    $media_url = $obj['results'][0]['media_url'];

    #echo $json;
    echo "<br /><br />";
    echo "searching for $search\n tweet count: $count\n";
    echo "<br /><br /><b>tweets</b><br />";
    echo "tweet_id: $id <br />";
    echo "user: $user <br />";
    echo "Tweet: $tweet<br />";
    echo "to_user: $to_user <br />";
    echo "media: $media_url";
    echo ""

?>
$host=”http://search.twitter.com";
$filename=“/search.json”;
$opts=array('http'=>array)(
'方法'=>“获取”,
'header'=>“接受语言:en\r\n”
));
$context=stream\u context\u create($opts);
$search=“mashable”;
$search=str_replace(“,“%20”,“$search”);
$count=“10”;
$a=“$host$filename?q=$search&rpp=$count&include\u entities=true”;
回显“$a\n”;
$json=file\u get\u contents($a,false,$context);
$obj=json_decode($json,true);
$id=$obj['results'][0]['id'];
$tweet=$obj['results'][0]['text'];
$user=$obj['results'][0]['from_user'];
$to_user=$obj['results'][0]['to_user'];
$media_url=$obj['results'][0]['media_url'];
#echo$json;
回声“

”; echo“搜索$search\n推特计数:$count\n”; 回声“

推特
”; 回声“tweet_id:$id
”; 回显“用户:$user
”; echo“Tweet:$Tweet
”; echo“to_user:$to_user
”; echo“媒体:$media_url”; 回声“” ?>
我想提取以下值: -用户名(发件人) -推特(文本) -发送给用户(如果是回复) -媒体附件(图片)


代码正在运行,但出于某种原因,我只收到最新的tweet,而不是number($count)值。我也无法接收推特的媒体url值。我的问题是:怎么做

您必须围绕结果循环以获得所有结果,而不是其中一个,例如:

$host = "http://search.twitter.com";
$filename = "/search.json";

$opts = array('http' => array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n"
));
$context = stream_context_create($opts);

$search = "mashable";
$search = str_replace(" ", "%20", $search);
$count = "10";

$a = "$host$filename?q=$search&rpp=$count&include_entities=true";
echo "$a\n";
$json = file_get_contents($a, false, $context);

$obj = json_decode($json, true);

foreach ($obj['results'] as $index => $result) {
    $id = $result['id'];
    $tweet = $result['text'];
    $user = $result['from_user'];
    $to_user = $result['to_user'];
    $media_url = $result['media_url'];

    #echo $json;
    echo "<br /><br />";
    echo "searching for $search\n tweet count: $count\n";
    echo "<br /><br /><b>tweets</b><br />";
    echo "tweet_id: $id <br />";
    echo "user: $user <br />";
    echo "Tweet: $tweet<br />";
    echo "to_user: $to_user <br />";
    echo "media: $media_url";
    echo "";
}
$host=”http://search.twitter.com";
$filename=“/search.json”;
$opts=array('http'=>array)(
'方法'=>“获取”,
'header'=>“接受语言:en\r\n”
));
$context=stream\u context\u create($opts);
$search=“mashable”;
$search=str_replace(“,“%20”,“$search”);
$count=“10”;
$a=“$host$filename?q=$search&rpp=$count&include\u entities=true”;
回显“$a\n”;
$json=file\u get\u contents($a,false,$context);
$obj=json_decode($json,true);
foreach($obj['results']作为$index=>$result){
$id=$result['id'];
$tweet=$result['text'];
$user=$result['from_user'];
$to_user=$result['to_user'];
$media_url=$result['media_url'];
#echo$json;
回声“

”; echo“搜索$search\n推特计数:$count\n”; 回声“

推特
”; 回声“tweet_id:$id
”; 回显“用户:$user
”; echo“Tweet:$Tweet
”; echo“to_user:$to_user
”; echo“媒体:$media_url”; 回声“; }

还有,当我做了一个
print\r($obj)我在那里看不到任何
媒体url
值-看起来Twitter不会返回该值,这就是为什么您无法访问它的原因。

您必须循环搜索结果以获得所有结果,而不是其中一个,例如:

$host = "http://search.twitter.com";
$filename = "/search.json";

$opts = array('http' => array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n"
));
$context = stream_context_create($opts);

$search = "mashable";
$search = str_replace(" ", "%20", $search);
$count = "10";

$a = "$host$filename?q=$search&rpp=$count&include_entities=true";
echo "$a\n";
$json = file_get_contents($a, false, $context);

$obj = json_decode($json, true);

foreach ($obj['results'] as $index => $result) {
    $id = $result['id'];
    $tweet = $result['text'];
    $user = $result['from_user'];
    $to_user = $result['to_user'];
    $media_url = $result['media_url'];

    #echo $json;
    echo "<br /><br />";
    echo "searching for $search\n tweet count: $count\n";
    echo "<br /><br /><b>tweets</b><br />";
    echo "tweet_id: $id <br />";
    echo "user: $user <br />";
    echo "Tweet: $tweet<br />";
    echo "to_user: $to_user <br />";
    echo "media: $media_url";
    echo "";
}
$host=”http://search.twitter.com";
$filename=“/search.json”;
$opts=array('http'=>array)(
'方法'=>“获取”,
'header'=>“接受语言:en\r\n”
));
$context=stream\u context\u create($opts);
$search=“mashable”;
$search=str_replace(“,“%20”,“$search”);
$count=“10”;
$a=“$host$filename?q=$search&rpp=$count&include\u entities=true”;
回显“$a\n”;
$json=file\u get\u contents($a,false,$context);
$obj=json_decode($json,true);
foreach($obj['results']作为$index=>$result){
$id=$result['id'];
$tweet=$result['text'];
$user=$result['from_user'];
$to_user=$result['to_user'];
$media_url=$result['media_url'];
#echo$json;
回声“

”; echo“搜索$search\n推特计数:$count\n”; 回声“

推特
”; 回声“tweet_id:$id
”; 回显“用户:$user
”; echo“Tweet:$Tweet
”; echo“to_user:$to_user
”; echo“媒体:$media_url”; 回声“; }

还有,当我做了一个
print\r($obj)
我在那里看不到任何
媒体url
值-看起来Twitter没有返回该值,这就是为什么您无法访问它。

我找到了接收每条推特的媒体url参数的解决方案:

$media_url = $result['entities']['media'][0]['media_url']

我找到了接收每条tweet的media_url参数的解决方案:

$media_url = $result['entities']['media'][0]['media_url']

谢谢你的回答!如果您将代码与“GettyImages”一起使用,您将在第二条tweet中看到参数media_url。谢谢您的回答!如果您将代码与“GettyImages”一起使用,您将在第二条tweet中看到参数media_url。