0

I want to get a json array from an API. Why cant I get it using file_get_contents($g_7_14)?

This is the error I get:

( ! ) Warning: file_get_contents(https://example.com:443/api/xxx/yyy?app_ids=666&start_date=2015-07-14&end_date=2015-07-14&auth_token=8sCR): failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable in C:\wamp\www\PHPExcel\index.php on line 8

and this is my code:

<?php 
//494792609

    if ( !empty ( $_GET['app_id'] ) ) {
        $g_7_14 = 'https://example.com:443/api/xxx/yyy?app_ids=666&start_date=2015-07-14&end_date=2015-07-14&auth_token=8sCR';
        echo $g_7_14.'<br>';

        $json_g_7_14 = file_get_contents($g_7_14);

        /* $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL,$g_7_14);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip')); 


         $xml_response = curl_exec($ch);


        var_dump($xml_response);
        */

        $a_g_7_14 = json_decode($json_g_7_14, true);

    }



?>


<!DOCTYPE html>
<html>
<head>
    <title>YH </title>
</head>
<body>

<form action="">
    <input type="text" name="app_id" />
    <button type="submin"> submit</button>
</form>

<div>

    <?php 

    ?>

</div>


</body>
</html>

You can see in the code I got a cURL part which is commented. Didn't work too :( !

Imnotapotato
  • 5,308
  • 13
  • 80
  • 147
  • Tested your code and got 404. Are you sure it returns JSON string? – al'ein Aug 19 '15 at 12:52
  • 406 Explanation : 406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request. – geggleto Aug 19 '15 at 13:01
  • Based on the info from @geggleto, you need to send a proper `Accepts` header. I assume your `curl` attempt work, and, if so, it's because it sends `Accepts: */*` by default. If you're bent on using `file_get_contents`, then use this answer to set up a context with headers: http://stackoverflow.com/questions/2107759/php-file-get-contents-and-headers – JAAulde Aug 19 '15 at 13:16
  • @AedixRhinedale yes, it looks like json, and my chrome json extention changes it's styling to how it usually change json stuff... – Imnotapotato Aug 19 '15 at 13:19
  • @geggleto i'm not sure I understand your answer.. – Imnotapotato Aug 19 '15 at 13:20
  • The `Accepts` header ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html ) of an HTTP request tells the server what kind of content is considered acceptable as a response. If the server cannot generate a content type that matches, it should send a HTTP response code of 406. It would seem that you are either missing the `Accepts` header in your request, or it is being set to something which the server cannot produce. I further assume that whatever API you are communicating with is very strict on this matter. Knowing what API you're talking to and seeing its docs would help us troubleshoot. – JAAulde Aug 19 '15 at 13:25
  • @JAAulde I added the $opt and $context thing to my code: `// Create a stream $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "Cookie: foo=bar\r\n" ) ); $context = stream_context_create($opts); $json_g_7_14 = file_get_contents($g_7_14, false, $context); $a_g_7_14 = json_decode($json_g_7_14, true);` not working still – Imnotapotato Aug 19 '15 at 13:27
  • It's a new API that Sensor Tower released a few days ago.no documentations ect' :( – Imnotapotato Aug 19 '15 at 13:28
  • You still didn't provide the `Accepts` header. Try this: `$context_opts = array('http' => array('method' => "GET",'header' => "Accepts: application/json\r\n")); $context = stream_context_create($context_opts); $json_g_7_14 = file_get_contents($g_7_14, false, $context); $a_g_7_14 = json_decode($json_g_7_14, true);` If that doesn't work, you need to contact the API vendor. – JAAulde Aug 19 '15 at 13:36
  • I still get the same error – Imnotapotato Aug 19 '15 at 13:39
  • Ok, I'm sending them an email about it, but I need to understand theoretically what's happening here, I'm new to PHP it's my first server language. and I want to understand what's happening in deep explaned theory as much possible. can you please try to explane what's happening in each request we are doing? – Imnotapotato Aug 19 '15 at 13:49
  • by the way the curl didn't work for me too – Imnotapotato Aug 19 '15 at 13:52
  • It has nothing to do with PHP--it's the HTTP transaction to the API server. Tell them you're making a request to their API (tell the the end point and params and such), and tell them the response you're getting back. – JAAulde Aug 19 '15 at 16:27

1 Answers1

0

Solved! I got the token from the company I work with.

Imnotapotato
  • 5,308
  • 13
  • 80
  • 147