简体   繁体   English

使用PHP的Curl API请求 - 长响应

[英]Curl API Request with PHP - long response

I try to get some gamescore information from stats.nba.com via curl : 我尝试通过curl从stats.nba.com获取一些游戏核心信息:

function getGame($gameID)
{
$url = "http://stats.nba.com/stats/boxscoretraditionalv2?EndPeriod=10&EndRange=28800&GameID=00" . intval($gameID) . "&RangeType=0&Season=2016-17&SeasonType=Regular+Season&StartPeriod=1&StartRange=0";
$process = curl_init($url);

curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($process, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);

$return = curl_exec($process);
$results = json_decode($return);
curl_close($process);

return $results;
}

In browser this information looks like this: 在浏览器中,此信息如下所示:

http://stats.nba.com/stats/boxscoretraditionalv2?EndPeriod=10&EndRange=28800&GameID=0021600966&RangeType=0&Season=2016-17&SeasonType=Regular+Season&StartPeriod=1&StartRange=0 http://stats.nba.com/stats/boxscoretraditionalv2?EndPeriod=10&EndRange=28800&GameID=0021600966&RangeType=0&Season=2016-17&SeasonType=Regular+Season&StartPeriod=1&StartRange=0

and takes about 1-2 seconds to load. 并且需要大约1-2秒才能加载。 But via php it takes up to 10-12 seconds to get information. 但是通过php,获取信息需要10-12秒。 curl_getinfo() shows that starttransfer_time is at 10 seconds: curl_getinfo()显示starttransfer_time为10秒:

array(26) {
 ["url"]=>
 string(174) "http://stats.nba.com/stats/boxscoretraditionalv2?EndPeriod=10&EndRange=28800&GameID=0021601068&RangeType=0&Season=2016-17&SeasonType=Regular+Season&StartPeriod=1&StartRange=0"
 ["content_type"]=>
 string(31) "application/json; charset=utf-8"
 ["http_code"]=>
 int(200)
 ["header_size"]=>
 int(384)
 ["request_size"]=>
 int(284)
 ["filetime"]=>
 int(-1)
 ["ssl_verify_result"]=>
 int(0)
 ["redirect_count"]=>
 int(0)
 ["total_time"]=>
 float(10.717)
 ["namelookup_time"]=>
 float(0.046)
 ["connect_time"]=>
 float(0.109)
 ["pretransfer_time"]=>
 float(0.109)
 ["size_upload"]=>
 float(0)
 ["size_download"]=>
 float(5455)
 ["speed_download"]=>
 float(509)
 ["speed_upload"]=>
 float(0)
 ["download_content_length"]=>
 float(5455)
 ["upload_content_length"]=>
 float(-1)
 ["starttransfer_time"]=>
 float(10.686)
 ["redirect_time"]=>
 float(0)
 ["redirect_url"]=>
 string(0) ""
 ["primary_ip"]=>
 string(13) "87.245.194.98"
 ["certinfo"]=>
 array(0) {
 }
 ["primary_port"]=>
 int(80)
 ["local_ip"]=>
 string(12) "192.168.1.88"
 ["local_port"]=>
 int(62105)
}

What could be a reason for that and how to fix it? 可能是什么原因以及如何解决?

Indeed, like @darker pointed out in the comments. 的确,就像@darker在评论中指出的那样。 it seems that adding the following line to your code does the trick. 似乎在你的代码中添加以下行就可以了。 (Tested). (测试)。

curl_setopt($process, CURLOPT_HTTPHEADER, array('Expect: 100-continue'));

Expect: 100-continue generally seems relevant when using POST. Expect: 100-continue在使用POST时通常看起来很相关。 Although, in your code you use GET and not POST, it may seem that the API you are using may have been designed to support otherwise. 虽然,在您的代码中,您使用的是GET而不是POST,但您使用的API似乎可能是为了支持而设计的。

In my research, I ran into an article (link below) which explains on how the transaction works. 在我的研究中,我遇到了一篇文章(下面的链接),它解释了交易的工作原理。

Quoting from the article, 引用文章,

API POST requests that include the Expect: 100-Continue header save bandwidth between the client and the server, because the server can reject the API request before the request body is even transmitted. 包含Expect:100-Continue标头的API POST请求可以节省客户端和服务器之间的带宽,因为服务器可以在请求主体甚至传输之前拒绝API请求。 For API POST requests with very large request bodies (such as file uploads), the server can, for example, check for invalid authentication and reject the request before the push body was sent, resulting in significant bandwidth savings. 对于具有非常大的请求主体(例如文件上载)的API POST请求,服务器可以例如检查无效的身份验证并在发送推送体之前拒绝该请求,从而显着节省带宽。

Reference: https://support.urbanairship.com/hc/en-us/articles/213492003--Expect-100-Continue-Issues-and-Risks 参考: https//support.urbanairship.com/hc/en-us/articles/213492003--Expect-100-Continue-Issues-and-Risks

First you need to ask for compressed response from the server through your request header. 首先,您需要通过请求标头询问服务器的压缩响应。

Secondly, interesting for your particular URL the server relies on the Accept-Language: header to respond quickly. 其次,对于您的特定URL而言,服务器依赖于Accept-Language:标头来快速响应。

Here are the lines will make your response faster. 以下行将使您的响应更快。

curl_setopt($process, CURLOPT_HTTPHEADER, array("Accept-Language: en-US,en;q=0.8,bn;q=0.6"));
curl_setopt($process,CURLOPT_ENCODING , ""); // Means handle all encodings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM