简体   繁体   中英

Curl API Request with PHP - long response

I try to get some gamescore information from stats.nba.com via curl :

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

and takes about 1-2 seconds to load. But via php it takes up to 10-12 seconds to get information. curl_getinfo() shows that starttransfer_time is at 10 seconds:

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. 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. 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.

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. 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.

Reference: 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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