lit*_*lab 4 php google-api google-api-php-client youtube-data-api
我正在使用这个库从 WordPress 网站获取 Youtube 的视频和直播。 https://github.com/google/google-api-php-client
代码在过去一个月工作正常,但突然开始随机出现连接失败错误(十次尝试中有两次失败,其他都正常)
这是我收到错误的代码:
function googleapis_get_live_event_widget(){
//Youtube Videos and Live Events
require_once( get_stylesheet_directory() . '/lib/google-api-php-client/src/Google/autoload.php' );
$GOOGLE_DEVELOPER_KEY = get_field('cce_api_google_developer_key','option');
$client = new Google_Client();
$client->setDeveloperKey($GOOGLE_DEVELOPER_KEY);
$youtube = new Google_Service_YouTube($client);
try {
$islive = get_field('cce_live_event','option');
$opts = array();
$opts['channelId'] = get_field('cce_api_youtube_channel_id','option');
if($islive) {
$opts['eventType'] = 'live';
}
$opts['type'] = 'video';
$opts['maxResults'] = 1;
$opts['order'] = 'date';
$live_videos = $youtube->search->listSearch('id,snippet',$opts);
$data = array();
$data['live_events'] = $live_videos;
echo Timber::compile('partials/live_event_widget.twig',$data);
} catch (Exception $e) {
$error = array();
$error['message'] = $e->getMessage();
echo Timber::compile('partials/error.twig',$error);
return;
}
}
add_shortcode('googleapis_live_widget','googleapis_get_live_event_widget');
Run Code Online (Sandbox Code Playgroud)
在服务器中执行 curl -v https://www.googleapis.comvia ssh 会导致尝试失败:
[~]# curl -v https://www.googleapis.com
* About to connect() to www.googleapis.com port 443 (#0)
* Trying 216.58.216.202... Connection timed out
* Trying 2607:f8b0:4009:80b::200a... Failed to connect to 2607:f8b0:4009:80b::200a: Network is unreachable
* Success
* couldn't connect to host
* Closing connection #0
curl: (7) Failed to connect to 2607:f8b0:4009:80b::200a: Network is unreachable
Run Code Online (Sandbox Code Playgroud)
当请求良好时,就像这样:
curl -v https://www.googleapis.com
* About to connect() to www.googleapis.com port 443 (#0)
* Trying 216.58.216.106... connected
* Connected to www.googleapis.com (216.58.216.106) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_RSA_WITH_AES_128_CBC_SHA
* Server certificate:
* subject: CN=*.googleapis.com,O=Google Inc,L=Mountain View,ST=California,C=US
* start date: Oct 21 22:31:24 2015 GMT
* expire date: Jan 19 00:00:00 2016 GMT
* common name: *.googleapis.com
* issuer: CN=Google Internet Authority G2,O=Google Inc,C=US
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.googleapis.com
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Date: Sat, 31 Oct 2015 11:33:49 GMT
< Vary: X-Origin
< Content-Type: text/html; charset=UTF-8
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Server: GSE
< Alternate-Protocol: 443:quic,p=1
< Alt-Svc: quic=":443"; p="1"; ma=604800
< Accept-Ranges: none
< Vary: Origin,Accept-Encoding
< Transfer-Encoding: chunked
<
* Connection #0 to host www.googleapis.com left intact
* Closing connection #0
Run Code Online (Sandbox Code Playgroud)
此时,如果我运行该页面,提要会正常显示。
我应该寻找什么来解决不一致问题?
我遇到了同样的错误并试图找出原因。
这个错误有一定的可能性。
您的互联网防火墙阻止了与 google 的连接:请
ping android.googleapis.com在您的 laravel 主机中尝试。或者curl -v gcm-http.googleapis.com如果没有响应则被阻止。
打开你的端口以便 google api 工作,我通常打开端口443,5528-2230。您可以在防火墙中进行配置
有gcm-http.googleapis.com很多 IP 地址,因此您需要允许来自这些 IP 地址的连接,这是它们的 IP 地址之一:172.217.3.170。我的解决方案是允许其 IP 地址范围之间的连接。您可以在防火墙软件中监控 IP 地址。
这取决于您的防火墙配置,您需要与配置防火墙的人员一起设置这些配置,因为我假设您在安全的网络环境中工作。