PHP Curl request to Stackoverflow API PHP Curl request to Stackoverflow API curl curl

PHP Curl request to Stackoverflow API


It looks like you are most of the way there, although I don't think any of the headers you have are required. The main thing that is missing is setting the encoding, since all responses are compressed (see the General section of the doco).

This is a quick working example:

<?php$stack_url = 'https://api.stackexchange.com/2.2/info?site=stackoverflow';$string  = curl_init($stack_url);curl_setopt($string, CURLOPT_ENCODING, 'gzip');  // Required by APIcurl_setopt($string, CURLOPT_RETURNTRANSFER, 1 );$result   = curl_exec($string );curl_close($string );var_dump($result );

If you are doing a bit more work with the API, you may want to look at a library to manage most of the work like Stack.PHP