How to access the contents of a Curl request with php?

Asked

Viewed 1,201 times

0

I’m developing a php bot that takes the posts from a facebook page feed using Curl via GET request. I want to extract certain information from the page to then put in content and display on a good site that part I roll, but I don’t know if the Curl function returns an array or a string seems to me to be a string my doubt is Curl has some function to access the data returned by it or have to parse 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);
?>

1 answer

0


As I understand it, this API returns a JSON and by default the parameter CURLOPT_RETURNTRANSFER of curl_setopt does the curl_exec return a string even if successful, according to documentation: http://php.net/manual/en/function.curl-setopt.php.
Therefore, only you decode the returned JSON string and if you want it to become an array, set the second parameter of the function as 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);    
  • mano she returns a json even I gave a var_dump and returned me a json that was even worth @Marcelo Bicudo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.