skip to Main Content

I have successfully fetched data from twitter API in the form of JSON. But the time provided by twitter is still not in the dateformat I need, so I think I have to edit it first. The problem is I don’t know how to edit a JSON file with PHP.

Here is what twitter API will give in the form of json

Now if I want to edit the ‘created_at’, how do I do it? So far, here’s what I got.

$results = search($query);

header('Content-Type: application/json'); 
$results= json_encode($results, JSON_PRETTY_PRINT);
echo $results;
file_put_contents('json_result.json', $results);

$contents = file_get_contents('json_result.json');
$contentsDecoded = json_decode($contents, true);
echo $contentsDecoded['statuses']->created_at;
foreach ($contentsDecoded['statuses'] as $tweet) {
	$date = new DateTime($tweet->created_at);
   	$formatted_date = $date->format("d-m-Y");
   	echo "Date ".$formatted_date;
}

I still get error saying that it’s a property of non-object…

2

Answers


  1. Edit it before putting it in the file instead of putting it, getting it again, saving again.

    Login or Signup to reply.
  2. I think the problem lies in one of these lines:

    echo $contentsDecoded['statuses']->created_at;
    foreach ($contentsDecoded['statuses'] as $tweet) {
    

    Either $contentsDecoded[‘statuses’] has property created_at or is an array
    So accessing property created_at of an array is not valid. Try to remove that line

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search