Search code examples
phpjsonwordpressinstagraminstagram-api

Why is my instagram api fetching data returning false instead of a json object?


I'm trying to use the instagram api on a wordpress page. Given the following function to get the data, would anyone please point me why is my var_dump($results) return boolean false???

function fetchData($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $result = curl_exec($ch);
    curl_close($ch); 
    return $result;
}


$result = fetchData("https://api.instagram.com/v1/users/{{userId}}/media/recent/?access_token={{accessToken}}&count=20");

var_dump($result);

note: {{userId}} & {{accessToken}} are placeholders. On my code Im using the real id and token instead.


Solution

  • Normally, Instagram Server will return

    {"pagination":{....},"meta":{"code":200},"data":[.......]}
    

    I think there is a SSL error, try to add this line below CURLOPT_TIMEOUT

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

    About SSL error

    cURL PHP RESTful service always returning FALSE

    You can try curl_error() and curl_errno() to debug cURL functions

    curl_exec() always returns false