Php Curl在本地计算机上返回数据,但在服务器上布尔值为false [英] Php Curl returns data on local machine but bool false on server

查看:271
本文介绍了Php Curl在本地计算机上返回数据,但在服务器上布尔值为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Facebook访问令牌

I am trying to get facebook access token

但是从浏览器和本地计算机php脚本执行时,URL返回真实的数据值。但是,当我从服务器运行相同的脚本时,它失败并显示bool(false)

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)

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.

这里有什么问题?

推荐答案

您应在卷曲错误,而不是仅查看curl_exec结果:

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 —返回一个字符串,其中包含当前会话的最后一个错误

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

请尝试以下操作:

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


curl_close($ch);

更新

尝试设置以下内容:

'curl' => [ CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4]

有关更多详细信息,请参见此处

For more details check here

这篇关于Php Curl在本地计算机上返回数据,但在服务器上布尔值为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆