dongmao3148 2019-05-24 11:17
浏览 138

如何在添加新推文时解码twitter JSON?

I hope that you can help me with this one. Basically what I am doing, is adding tweets to my twitter account through api from my website administration panel.

It all works fine, BUT I cannot store the info to database because the json_decode variable that I have returns a NULL. I do not understand what is the appropriate way to get the info from twitter json.

Here is my code:

    $settings = array(
    'oauth_access_token' => $oauth_access_token,
    'oauth_access_token_secret' => $oauth_access_token_secret,
    'consumer_key' => $consumer_key,
    'consumer_secret' => $consumer_secret
    );

    $connection = new TwitterAPIExchange($settings);
    $url = 'https://api.twitter.com/1.1/statuses/update.json';

    $requestMethod = "POST";
    $user = $twitter_user;

    $postfields = array(
        'screen_name' => $user,
        'status' => $twit ); 

    $connection->buildOauth($url, $requestMethod)
                 ->setPostfields($postfields)
                 ->performRequest();


    // get the contents of the JSON file 
    $jsonCont = file_get_contents($connection);

    //decode JSON data to PHP array
    $content = json_decode($jsonCont, true);

    $conn = new mysqli($server, $username, $password, $database);

    $id = $content['id'];
    $created = $content['created_at'];
    $tweet = $content['text'];
    $by = $content['user']['name'];

    $sql = "INSERT INTO `twitter` (`idtwit`, `tweet`, `tweetedby`, `datet`) VALUES ('".$id."', '".$tweet."', '".$by."', '".$created."')";
    $conn->query($sql);
    echo "<br /><div class='alert alert-info'>New record updated successfully</div>";

    $conn->close();

How to use json_decode properly with this ?!

I have also tried to wrap it around the $connection request like so, eliminating my variable, but then, it will not post to twitter anymore.

$connection->json_decode(buildOauth($url, $requestMethod)
                 ->setPostfields($postfields)
                 ->performRequest(),$assoc = TRUE);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?