dryl34156 2016-02-25 19:41
浏览 294

当我应该使用php curl获取状态304时获取HTTP状态200

I'm building an application that makes frequent get calls to an external api and caches the data that is sent back. The api also sends "requestnext" url, last modified date, and ETag that can be used to make conditional get calls for updates. When making a requestnext call, I should be receiving a status 304 when there is no new data, but I'm only getting status 200 (even if there is no new data).

My code:

$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'];

The $lastmod and $etag variables contain

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

Any ideas for what I might be doing wrong?

  • 写回答

1条回答 默认 最新

  • dongwo2222 2016-02-26 01:49
    关注

    Turns out I was sending the conditional get request to the wrong url. I should've been sending it to the original url I used to make the request (I was sending it to the "nextrequest" url instead).

    This was for the AP Elections api in case anyone was wondering!

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)