【问题标题】:Getting HTTP status 200 when I should be getting status 304 using php curl当我应该使用 php curl 获取状态 304 时获取 HTTP 状态 200
【发布时间】:2016-02-25 19:41:18
【问题描述】:

我正在构建一个应用程序,该应用程序经常对外部 api 进行 get 调用并缓存发回的数据。该 api 还发送“requestnext” url、最后修改日期和 ETag,可用于进行有条件的 get 调用以进行更新。在进行 requestnext 调用时,如果没有新数据,我应该会收到状态 304,但我只会收到状态 200(即使没有新数据)。

我的代码:

$curl = curl_init();

                curl_setopt_array($curl, array(
                    CURLOPT_RETURNTRANSFER => 1,
                    CURLOPT_URL => $nextrequest . '&apiKey='.$apikey,
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_HTTPHEADER => array($lastmod, $etag),
                    CURLOPT_HEADER => 1,
                ));

                $response = curl_exec($curl);

                $curlinfo = curl_getinfo($curl);

                // Get http response code
                $httpresponse = $curlinfo['http_code'];

$lastmod 和 $etag 变量包含

$lastmod = If-Modified-Since: Wed, 24 Feb 2016 22:38:22 GMT
$etag = If-None-Match: "ffffffffec3c286a-json"

关于我可能做错的任何想法?

【问题讨论】:

  • 你有没有向运行外部 API 的人提出这个问题?
  • 刚刚联系了他们,谢谢!等我弄明白了再更新

标签: php http curl


【解决方案1】:

原来我将条件获取请求发送到了错误的 url。我应该将它发送到我用来发出请求的原始网址(我将它发送到“nextrequest”网址)。

这是为 AP Elections api 准备的,以防有人想知道!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 2016-10-24
    • 2011-08-13
    • 2018-03-23
    • 2021-12-03
    • 2010-12-12
    相关资源
    最近更新 更多