1

I'm new to JSON and APIs and I want to make a PHP CLI script that will use StackOverflow API. I've made myself a key and this is my code so far:

<?php

//Call stack API .$key
$google_url = "http://api.stackoverflow.com/1.1/stats";

//Get and Store API results into a variable
$result = file_get_contents($google_url);
//$jsonArray = json_decode($result);
print_r($result);
//var_dump($jsonArray);

?>

But the print_r($result); returns some strange characters. I can't read it could you give me some info? Any help will be appreciated. The whole project will be an open source search engine about StackOverflow posts and comments!

2
  • 1
    You're probably getting the gzip compressed form of the data. If you use cURL instead of file_get_contents(), you can tweak the options so it handles gzip for you. Commented Dec 21, 2011 at 15:51
  • thanks a lot that was the problem indeed Commented Dec 22, 2011 at 1:14

1 Answer 1

2

You need to un gzip it even if you didn't send an accept: gzip header in the http request, because all responses are gzip'd

see How can I read GZIP-ed response from Stackoverflow API in PHP?

Sign up to request clarification or add additional context in comments.

1 Comment

OOOO you rock man thanks a lot! that save my problem! One more question so i want to get a question with it's comments using this API how can i get it and store them on an array!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.