Problem with cURL

Hello,

this is not kirby related but maybe somebody can help me out:

If i open this URL with a browser i get a nice json response:

https://graph.facebook.com/oauth/access_token?redirect_uri=http://www.domain.com&client_id=456&client_secret=123&grant_type=client_credentials

gives:

{
   "error": {
      "message": "Error validating application. Cannot get application info due to a system error.",
      "type": "OAuthException",
      "code": 101,
      "fbtrace_id": "Au86iVmLm9u"
   }
}

a error message as expected but anyway… if i load this URL in PHP with curl or file_get_contents i always get:

  ["errno"]=>
  int(7)
  ["errmsg"]=>
  string(0) ""
  ["content"]=>
  bool(false)

“errno7” i a “no route to host” error.

Why? How can i send a php curl request to get the same response as in a browser? I have tested lots of CURL_OPTs with always the same result: no route to host.

Here is some PHP code to test it. Just copy&paste the dumped URL to a browser tab… it will give you nice JSON.

$url = "https://graph.facebook.com/oauth/access_token?redirect_uri=http://www.domain.com&client_id=456&client_secret=123&grant_type=client_credentials";

    $options = array(
        //CURLOPT_VERBOSE => 1,
        CURLOPT_RETURNTRANSFER => true,     // return web page
        //CURLOPT_HEADER         => true,    // don't return headers
        //CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        //CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36", // who am i
        //CURLOPT_AUTOREFERER    => false,     // set referer on redirect
        //CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        //CURLOPT_TIMEOUT        => 120,      // timeout on response
        //CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        //CURLOPT_IPRESOLVE      => true,
        //CURL_IPRESOLVE_V4      => true,
        //CURLOPT_SSL_VERIFYPEER => false,     // Disabled SSL Cert checks
        //CURLOPT_SSL_VERIFYHOST =>  2,
        //CURLOPT_SSLVERSION => 3,
        //CURLOPT_CAINFO         => "*facebookcom.crt",
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    echo("<pre>");var_dump($header);

:confused:

greetings,
svnt

“No route to host” means that your web server can’t connect to Facebook’s. That it works locally does not mean anything as you’d need to test directly on your server to compare it.

Maybe outgoing requests are blocked by your hosting provider?

It does not work locally. It seems FB identifies a request by cURL and blocks it.
I’m still looking for a solution.

S.

“No route to host” is an error that already occurs on the local machine, it’s related to DNS so it can’t really be that Facebook blocks access.

Could you please try on a different machine/server? Also try clearing your DNS cache.

I did a brief test with your code and no problems at all.

but with valid parameters i get still a strange “no route to host”… damn FB… i don’t want to use this bloated FB PHP SDK… … . :frowning:

works again. Maybe the issue hit me due a facebook API change from 2.8 to 2.9.

<?php
// https://www.willconkwright.com/how-to-get-public-data-from-facebook-with-php/

$appID = '12345';
$appSecret ='6666666666666666666'; 
$pageID = '9999999999';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'client_id='.$appID.'&client_secret='.$appSecret.'&grant_type=client_credentials');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "fbcookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "fbcookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
$response = curl_exec($ch) or die(curl_error($ch));
$tmp = json_decode($response);
$accessToken = $tmp->{'access_token'};

$url = 'https://graph.facebook.com/v2.9/821494507901839/events/attending/?fields=id,name,description,place,timezone,start_time,end_time,cover&limit=999&access_token='.$accessToken.'&since=0&until=2033-12-02';
echo $url;
$response = file_get_contents($url);
$data = json_decode($response, true);

foreach($data["data"] as $event) {

	$coverImg = $event["cover"]["source"];

  	echo('<img src="'.$coverImg.'" style="width:100px;">');

}