Php Curl returns data on local machine but bool false on server

浪子不回头ぞ 提交于 2020-01-15 11:11:23

问题


I am trying to get facebook access token

But the url when executed from browser and local machine php script returns true data values. However, when i run the same script from server, it fails and shows bool(false)

the url

   $token_url = "https://graph.facebook.com/oauth/access_token?"
    . "client_id=".$config['appId']."&redirect_uri=" . urlencode($config['callback_url'])
    . "&client_secret=".$config['secret']."&code=" . $_GET['code'];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $token_url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $response = curl_exec ($ch);

    var_dump($response);

Other urls work on the server.

What is wrong here ?


回答1:


You should check at the curl error instead of only looking at the curl_exec result:

PHP 4 >= 4.0.3, PHP 5, PHP 7) curl_error — Return a string containing the last error for the current session

Try this:

if(curl_exec($ch) === false) {
    echo 'Curl error: ' . curl_error($ch);
} else {
    echo 'Operation completed without any errors';
}


curl_close($ch);

UPDATE

Try setting this:

'curl' => [ CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4]

For more details check here




回答2:


Try to check curl_error

curl_error — Return a string containing the last error for the current session

That may help finding what was wrong .




回答3:


I have been given to understand from an upvoted answer on one of the other questions that my server Hostinger (free) does not allow connecting to Facebook SDK !!!

I am moving to a paid server !



来源:https://stackoverflow.com/questions/44161254/php-curl-returns-data-on-local-machine-but-bool-false-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!