0

Hi I have been trying to figure this out for hours. I am attempting (for learning purposes) to construct a very simple twitter feed with Twitters API. Most of the task I understand, but, passing the JSON into javascript is proving quite difficult without some form of error.

The main error is

Uncaught SyntaxError: Malformed arrow function parameter list

This is my PHP

<?php 

    require "twitteroauth/autoload.php";

    use Abraham\TwitterOAuth\TwitterOAuth;

    $consumerkey = "?";
    $consumersecret = "?";
    $access_token = "?";
    $access_token_secret = "?";

    $connection = new TwitterOAuth($consumerkey, $consumersecret, $access_token, $access_token_secret);
    $content = $connection->get("account/verify_credentials");

    $obj = $connection->get("statuses/home_timeline");

    $len = count((array)$obj);

    if (gettype($obj) == 'object')
    {
      print_r($obj->errors);
    } elseif (gettype($obj) == 'array')
    {
      $p = 0;
        for ($i = 0; $i < $len; $i++) 
        {
          if ($obj[$i]->favorite_count > 2) 
          {
            $favouriteTweets[$p] = $obj[$i];
            $p++;
          } 
        } 
    }


    ?>

and this is my JS

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <script type="text/javascript">

        var json = JSON_encode(<?php print_r($favouriteTweets); ?>);


    </script>

I have tried parsing the $favouriteTweets, I've tried encoding, decoding with different combinations. I've learned a lot but can't seem to fix the problem. Please help.

Dyneskye
  • 33
  • 5
  • 5
    PHP: `print_r()` does not output JSON. `json_encode()` does. JS: parsing is done via `JSON.parse()` (but not needed here). – Sirko Apr 27 '20 at 14:47
  • 2
    But the output wouldn't be JSON in that context, it would be a JavaScript literal. – Quentin Apr 27 '20 at 14:48

0 Answers0