You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP Curl调用接口有时返回空字符串且无错误的问题排查

问题分析:调用Bovada接口时偶尔返回空内容的原因

问题场景

调用Bovada的NFL赛事接口时,PHP Curl有时会返回空字符串(无任何错误提示),但使用Firefox或Postman发起请求时,每次都能成功获取完整的JSON数据。

调用代码

$ch = curl_init();
$headers = array(
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
    'Accept: application/json',
);
curl_setopt($ch, CURLOPT_URL,"https://www.bovada.lv/services/sports/event/v2/events/A/description/football/nfl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$contents=curl_exec($ch);
$json = json_decode($contents, true);
print_r($json);
echo curl_error($ch);
curl_close($ch);

Curl Verbose输出(返回空内容时)

*   Trying 157.185.178.148:443...
* Connected to www.bovada.lv (157.185.178.148) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: c:\php\cacert.pem
  CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=www.bovada.lv
*  start date: Jan  5 01:30:09 2023 GMT
*  expire date: Feb  6 01:30:08 2024 GMT
*  subjectAltName: host "www.bovada.lv" matched cert's "www.bovada.lv"
*  issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign GCC R3 DV TLS CA 2020
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x1e88f349bf0)
> GET /services/sports/event/v2/events/A/description/football/nfl HTTP/2
Host: www.bovada.lv
user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
accept: application/json

* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 200 
< date: Tue, 12 Sep 2023 16:22:57 GMT
< content-length: 0
< x-varnish: 325065529
< set-cookie: TS01ed9118=014b5d5d07f3386e1d6b4faa70e50ad6021f9eccb5336e851c55d0c1bf5a020540f29b463d4d69b3f109a2c2206f136d64e4531720; Path=/; Domain=.www.bovada.lv
< age: 1535
< via: 1.1 PS-YUL-01c3G98:1 (W), 1.1 PS-YUL-01Ge696:2 (W), 1.1 hx172:6 (W), 1.1 ls25:5 (W)
< x-px: ht ls25DFW
< x-ws-request-id: 65009061_ls25_19321-37062
< strict-transport-security: max-age=16070400
< server: PWS/8.3.1.0.8
< cache-control: max-age=604800
< 
* Connection #0 to host www.bovada.lv left intact

Firefox请求情况

Firefox发起请求时,响应的Content-Length为112293,能正常获取完整的JSON数据。

原因分析

从Verbose输出的响应头可以看出几个关键线索:

  1. CDN缓存命中:响应头包含x-varnish(Varnish CDN标识)和age:1535(缓存内容已存在1535秒),说明空内容是CDN返回的缓存结果,而非源站直接响应。
  2. Cookie差异:Firefox会自动携带之前会话的Cookie,而你的Curl请求未复用或发送必要的Cookie(比如响应中返回的TS01ed9118 Cookie)。CDN通常会根据Cookie值区分缓存条目,无Cookie的请求命中了无效的空缓存。
  3. HTTP/2兼容性:Curl使用HTTP/2发起请求,而Firefox可能在某些场景下 fallback到HTTP/1.1,或者CDN对HTTP/2请求的缓存逻辑存在异常,导致空缓存生成。

解决方案建议

  • 复用Cookie:在后续请求中带上响应返回的set-cookie值,模拟浏览器的会话状态。
  • 强制使用HTTP/1.1:添加curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);,避免HTTP/2带来的缓存异常。
  • 禁用缓存:在请求头中加入Cache-Control: no-cache或Pragma: no-cache,强制CDN返回源站新鲜内容。

内容的提问来源于stack exchange,提问作者btorkelson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:46:02