Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 从twitter响应向json对象添加元素_Php_Json_Twitter - Fatal编程技术网

Php 从twitter响应向json对象添加元素

Php 从twitter响应向json对象添加元素,php,json,twitter,Php,Json,Twitter,我试图在arraay中添加属性,但我不断出现以下错误: 警告:json_decode()希望参数1是字符串,对象在第25行的C:\xampp\htdocs\4th\twitdb2\get_tweets.php中给出 警告:第27行的C:\xampp\htdocs\4th\twitdb2\get_tweets.php中为foreach()提供的参数无效 空的 我想在数组的每个元素的末尾添加一个广告,但我无法控制它。任何帮助都将不胜感激。多谢各位 <?php session_sta

我试图在arraay中添加属性,但我不断出现以下错误:

警告:json_decode()希望参数1是字符串,对象在第25行的C:\xampp\htdocs\4th\twitdb2\get_tweets.php中给出

警告:第27行的C:\xampp\htdocs\4th\twitdb2\get_tweets.php中为foreach()提供的参数无效 空的

我想在数组的每个元素的末尾添加一个广告,但我无法控制它。任何帮助都将不胜感激。多谢各位

  <?php
    session_start();
    require_once("twitteroauth.php"); //Path to twitteroauth library

    $search = "xxxxx";
    $geocode = "xxxxx";
    $notweets = xxxxxx;
    $consumerkey = "xxxxxxx";
    $consumersecret = "xxxxxxxx";
    $accesstoken = "xxxxx-xxxxxx";
    $accesstokensecret = "xxxxxxx";


    function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
      $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
      return $connection;
    }

    $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

    $search = str_replace("#", "%23", $search);
    $tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&geocode=".$geocode."&count=".$notweets);


    $result = json_decode($tweets, true);

    foreach($result as $key => $val)
    {
       $result[$key]['is_selected'] = 0;
    }
      echo json_encode($result);





    ?>

这应该可以做到:

$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&geocode=".$geocode."&count=".$notweets);

for($i = 0; $i < count($tweets->statuses); $i++) {
    $tweets->statuses[$i]['is_selected'] = 0;
}
echo json_encode($result);
$tweets=$connection->get(“https://api.twitter.com/1.1/search/tweets.json?q=“$search.”&geocode=“.$geocode.”&count=“.$notweets”);
对于($i=0;$istatus);$i++){
$tweets->status[$i]['is_selected']=0;
}
echo json_编码($result);

我认为问题在于,
$connection->get(…)
返回的不是纯json,而是一个已经解码的php对象,您可以循环使用。你能给我们看一个简短的变量转储吗?我仍然收到这个错误致命错误:不能在$tweets->statuses[$i]['is_selected']=0中使用stdClass类型的对象作为数组;我仍然收到此错误致命错误:无法在$tweets->statuses[$i]['is_selected']=0中将stdClass类型的对象用作数组;
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&geocode=".$geocode."&count=".$notweets);

for($i = 0; $i < count($tweets->statuses); $i++) {
    $tweets->statuses[$i]['is_selected'] = 0;
}
echo json_encode($result);