I work with api instagram in php, and so I need to get data about the user. In the office. documentation instagram> ( https://www.instagram.com/developer/authentication/ ), there is a line like this:

This is a sample request: curl -F 'client_id=CLIENT_ID' \ -F 'client_secret=CLIENT_SECRET' \ -F 'grant_type=authorization_code' \ -F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \ -F 'code=CODE' \ https://api.instagram.com/oauth/access_token 

So, as I understand it, this is indicated as an example, to get a token (correct if not so), I watched videos on php, and there they write:

 public function getAccessTokenAndUserDetails($code) { $postFields = array( "client_id" => $this->clientID, "client_secret" => $this->clientSecret, "grant_type" => "authorization_code", "redirect_uri" => $this->redirectURI, "code" => $code ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.instagram.com/oauth/access_token"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); } 

Well, already through the function we get access to it:
$data = $Instagram->getAccessTokenAndUserDetails($_GET['code']); print_r($data);
So, the question is, something does not work for me, that is, the json request is not displayed, shows null (via var_dump). What could be the error?

    1 answer 1

    In the example you give, when you return a value in the getAccessTokenAndUserDetails method, you use json_decode (). It returns null in two cases:

    1. Could not parse the answer (incorrect json or not json at all) - most likely
    2. The nesting level of the json array exceeds the set recursion depth — unlikely.

    So, check what answer the curl query returns to you, if it really is a valid json:

     public function getAccessTokenAndUserDetails($code) { $postFields = array( "client_id" => $this->clientID, "client_secret" => $this->clientSecret, "grant_type" => "authorization_code", "redirect_uri" => $this->redirectURI, "code" => $code ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.instagram.com/oauth/access_token"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); $response = curl_exec($ch); curl_close($ch); var_dump($response); // смотрим, что вернул curl return json_decode($response, true); }