How to access the contents of a curl-php request?

0

I'm developing a bot in php that takes the feed posts from a facebook page using curl via GET request. I want to extract certain information from the page to later put in content and display on a good site that part I unroll, but I do not know if the curl function returns an array or a string seems to be a string my doubts is the curl has some function to access the data returned by it or do you have to do a parse in the content? the bot request code:

<?php
    define("VERSAO", "/v2.10", TRUE);
    define("PAGINA", "/resultadojogodobicho", TRUE);
    define("GRAPH", "?fields=feed{full_picture,message}", TRUE);
    define("ACCESS_TOKEN", "&access_token=...", TRUE); 
    define("URL", "https://graph.facebook.com", TRUE);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, URL.VERSAO.PAGINA.GRAPH.ACCESS_TOKEN);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($ch);
    if($return){
        echo 1;
    }else{
        echo 0;
    }
    curl_close($ch);
?>
    
asked by anonymous 15.09.2017 / 02:10

1 answer

0

As I understand it, this API returns a JSON and by default the CURLOPT_RETURNTRANSFER parameter of curl_setopt causes curl_exec to return a string even in case of success, according to documentation: link . So, just decode the returned JSON string and if you want it to become an array, set the second parameter of the function to true . The script would look like this in your case:

$return = curl_exec($ch);
if($return){
    $json_decoded = json_decode($return, true);
}else{
    echo 0;
}
curl_close($ch);    
    
15.09.2017 / 02:51