Émi*_*ron 10 php ubuntu curl facebook-graph-api
我正在我正在研究的网站上集成Facebook登录选项,并且在用PHP调用Facebook的API时遇到了一些问题.我得到的实际错误如下:
无法连接到graph.facebook.com端口443:连接超时
我已经验证了curl调用的超时值是正确的,我尝试通过SSH直接从服务器的命令提示符检查连接(ping正确,端口似乎是打开的,进程正在监听它等).然而,使用我在网上找到类似问题的简单卷曲片段,我设法轻松测试它,似乎问题是间歇性/不一致的:有时它完美无瑕地工作,有时它会加载几秒钟因上述错误而失败.
我非常怀疑这个错误是在Facebook的一面,但我无法弄清楚它是什么我做错了.我以前在PHP中使用过Facebook SDK,这是我第一次看到这个错误.
有没有其他人在遇到这个问题并修复它?
快速说明:这是我第一次在基于Symfony的项目中使用Facebook - 不要认为它在这种情况下是相关的,而是为了以防万一而将其丢弃.
相关摘要:
$fb = new \Facebook\Facebook(['app_id' => '[withheld]',
'app_secret' => '[withheld]',
'default_graph_version' => 'v2.10',]);
$fb->setDefaultAccessToken($accessToken); # Access token obtained from Facebook Login in JS, passed in post data.
try {
$basic_info_response = $fb->get('/me?fields=id,first_name,last_name,email,website');
if ($with_picture)
$profile_picture_response = $fb->get('/me/picture?width=720&height=720');
if ($with_friends)
$friends_response = $fb->get('/me/friends');
}
catch(\Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch(\Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,它在任何$ fb-> get()调用中都失败 - 并不总是相同的,有时在第一个,有时在第一个,有时在朋友一个.
[更新]
大约90%的电话都出现了错误.我尝试通过SSH(curl -v graph.facebook.com
)直接在服务器上进行curl调用,得到以下两个结果:
* Rebuilt URL to: graph.facebook.com/
* Trying 31.13.91.2...
* TCP_NODELAY set
* Trying 2a03:2880:f01b:1:face:b00c:0:1...
* TCP_NODELAY set
* Immediate connect fail for 2a03:2880:f01b:1:face:b00c:0:1: Network is unreachable
* Connected to graph.facebook.com (31.13.91.2) port 80 (#0)
> GET / HTTP/1.1
> Host: graph.facebook.com
> User-Agent: curl/7.50.2
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api"
< Access-Control-Allow-Origin: *
< Pragma: no-cache
< Cache-Control: no-store
< x-fb-rev: 3274377
< Content-Type: application/json; charset=UTF-8
< x-fb-trace-id: A2OYZzFP3v8
< facebook-api-version: v2.4
< Expires: Sat, 01 Jan 2000 00:00:00 GMT
< Vary: Accept-Encoding
< X-FB-Debug: z9FEtq3Rlh8LyFn6pOIBZ5ZMCX+TY1jUD7iZ7ZRZ8/YGAsi035TbCP3qdBzqxDryvjJhKoKxnbAdvcxY/7r3Vg==
< Date: Mon, 04 Sep 2017 19:51:40 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host graph.facebook.com left intact
Run Code Online (Sandbox Code Playgroud)
和
* Rebuilt URL to: graph.facebook.com/
* Trying 31.13.91.2...
* TCP_NODELAY set
* Connected to graph.facebook.com (31.13.91.2) port 80 (#0)
> GET / HTTP/1.1
> Host: graph.facebook.com
> User-Agent: curl/7.50.2
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api"
< Access-Control-Allow-Origin: *
< Pragma: no-cache
< Cache-Control: no-store
< x-fb-rev: 3274377
< Content-Type: application/json; charset=UTF-8
< x-fb-trace-id: BrIDaH8D8D5
< facebook-api-version: v2.4
< Expires: Sat, 01 Jan 2000 00:00:00 GMT
< Vary: Accept-Encoding
< X-FB-Debug: tYvsf7Nn2PVnHFkV40UUddjQGKzPl8XKfdNeiqu1CXZck5WuUUlcG9hoCAZQrOX93uS19m2JpAEu9DJ/YhSIeg==
< Date: Mon, 04 Sep 2017 19:54:54 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host graph.facebook.com left intact
Run Code Online (Sandbox Code Playgroud)
有没有人对此问题有任何更多信息或可能的解释?
事实证明,CURLOPT_CONNECTTIMEOUT
除了常规超时之外,Facebook 的 SDK 中还设置了硬编码 - 这似乎是导致问题的原因。每当我的服务器和 Facebook API 之间的连接太慢(10 秒以上)时,就会超时,因为 10 秒是 SDK 中的默认值。
我将此值更改为 cURL 该选项的默认值,即 300,然后它再次开始工作。该值是在 Facebook 的 SDK 中的FacebookCurlHttpClient
类、openConnection
方法中设置的。
现在,导致连接有时如此缓慢的原因仍然未知,但至少在发生这种情况时它不会再崩溃。
您可以重新定义 SDK 的 cURL 包装器的方法,如下所示: https: //www.sammyk.me/how-to-inject-your-own-http-client-in-the-facebook-php-sdk-v5 #定制-curl-预定义常量
例如,这是我重新定义的方法,以允许超时前更长的时间:
<?php
namespace AppBundle\Lib\Facebook;
class CustomCurlOptsHttpClient extends \Facebook\HttpClients\FacebookCurlHttpClient
{
public function send($url, $method, $body, array $headers, $timeOut)
{
$timeOut *= 5;
return parent::send($url, $method, $body, $headers, $timeOut);
}
public function openConnection($url, $method, $body, array $headers, $timeOut)
{
$timeOut *= 5;
parent::openConnection($url, $method, $body, $headers, $timeOut);
$options = [
CURLOPT_CONNECTTIMEOUT => 300,
];
$this->facebookCurl->setoptArray($options);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您有多个请求一个接一个地发出,您还可以在 Facebook SDK 中使用批量请求。这将有助于加快该过程并防止您超时。
希望这对遇到同样问题的其他人有所帮助!
归档时间: |
|
查看次数: |
1339 次 |
最近记录: |