0

I use ParseHub to get data from an Ajax-based website. The data is returned in json file format. I have been struggling to print returned json file for hours, but could not be successful. Here is my php code:

<?php
header('Content-Type: application/json;charset=utf-8');

$params = http_build_query(array(
"api_key" => "xxxxx",
"format" => "json"
));

$result = file_get_contents(
'https://www.parsehub.com/api/v2/projects/{MY_RUN_TOKEN}/last_ready_run/data?'.$params,
false,
stream_context_create(array(
    'http' => array(
        'method' => 'GET'
    )
))
);

echo $result;


?>

By the way, json_decode() function didn't work. Can anyone help me?

UPDATE: I uploaded code and output of the code. You can check to give some ideas.

Code -> this link Output -> this link

umitkilic
  • 135
  • 2
  • 15

1 Answers1

1

From the ParseHub API documentation:

The Content-Encoding of this response is always gzip.

Try to use gzdecode function for your $result variable. And then decode it with json_decode().

Note that it requires PHP 5 >= 5.4.0 or PHP 7 version. Also you can try to use Parsehub REST api wrapper class.

camelsWriteInCamelCase
  • 1,625
  • 1
  • 7
  • 14
  • Yes gzdecode works. Thanks for your help. I added this line after getting $result : $data=gzdecode($result); echo $data; But gzdecode returns string. So $data is not a json. Now, the problem is converting string $data into json. :) – umitkilic Oct 27 '17 at 07:19
  • When i try to use: echo json_decode($data); it throws that error: "Catchable fatal error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\a.php on line 20" – umitkilic Oct 27 '17 at 07:30
  • I uploaded final code and output of the code. Can you check and give some idea please? Code -> this [link](https://imgur.com/egarydf) Output -> this [link](https://imgur.com/93DgSn2) – umitkilic Oct 27 '17 at 08:01
  • Actually, when i run the php code on the postman application and check result in json mode, i can see it in json format. I am not sure but the problem is looks like solved. Here is what i see: [link](https://imgur.com/ZImkd9Y) – umitkilic Oct 27 '17 at 08:14
  • 1
    Yes, this is JSON data. Use `json_decode($data, true);` to decode it. – camelsWriteInCamelCase Oct 27 '17 at 15:18
  • gzdecode() is solved my problem without using json_decode($data,true) function. If json_decode function is used, it return error that says "Catchable fatal error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\a.php on line 20". But without json_decode, it is okay. Thanks again for your advice. – umitkilic Oct 28 '17 at 21:59
  • Btw, var_dump($result) is string, it is not json. – umitkilic Oct 28 '17 at 22:27
  • If you want, you can ask their support about it. [Here](https://www.parsehub.com/docs/ref/api/v2/?plaintext#get-last-ready-data) they wrote that `format` can be `csv` or `json`. – camelsWriteInCamelCase Oct 28 '17 at 22:43
  • I have wrote already. But they said "use gzdecode()". Not much more than. – umitkilic Oct 28 '17 at 22:52