2017-03-15 18:08
浏览 345

PHP Curl - 跟随“201 Created”响应头的位置

I am submitting a CURL Post request to an API which, upon success, returns status "201 Created" with the URL of the resource in the LOCATION part of the header. What I'd like is to automatically retrieve the newly created resource but so far haven't been able to do so. I've tried setting curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); but to no avail. it's worth noting that the resource does require a GET request.

I'm not sure if it's the status code being a 201, or if the request method needs to change from POST to GET, but for some reason it's not following the LOCATION header. Fetching curl_getinfo($ch,CURLINFO_EFFECTIVE_URL); seems to confirm this as the result is the same as the original URL, not the new LOCATION.

As a last resort I have considered simply parsing the headers and creating a new CURL request, but this would not be optimal and I'm guessing that I'm just missing something simple to make this work as desired.

How can I get CURL to automatically follow and submit a GET request to the LOCATION returned with a 201 response?

图片转代码服务由CSDN问答提供 功能建议

我正在向API提交CURL Post请求,该请求在成功时返回带有URL的“201 Created”状态 标题的 LOCATION 部分中的资源。 我想要的是自动检索新创建的资源,但到目前为止还没有这样做。 我已经尝试设置 curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true); 但无济于事。 值得注意的是资源确实需要 GET 请求。

我不确定它的状态代码是201,还是请求方法需要 从 POST 更改为 GET ,但由于某种原因,它不遵循 LOCATION 标头。 获取 curl_getinfo($ ch,CURLINFO_EFFECTIVE_URL); 似乎确认这一点,因为结果与原始URL相同,而不是新的 LOCATION \ n


如何让CURL自动关注并提交 GET 请求到以201响应返回的 LOCATION \ n

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • douqiao4450 2017-03-15 18:19

    You can't.

    With curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl follow the response LOCATION only for 3xx status codes.

    AFAIK and stating as the documentation there is no way to force curl to follow-location with a 201 response.

    You have to parse the header, fetch the LOCATION and then issue a second curl request.

    Following a location with a status different than 3xx would be an anomaly. Also from the curl command line tool and C library documentation: -L, --location -- (HTTP) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place

    Giving a quick look at the curl source code you find on /lib/http.c the function Curl_http_readwrite_headers.

    Location follow is handled inside with this conditional:

    else if((k->httpcode >= 300 && k->httpcode < 400) &&
            checkprefix("Location:", k->p) &&
            !data->req.location) {
      /* this is the URL that the server advises us to use instead */
      char *location = Curl_copy_header_value(k->p);
        return CURLE_OUT_OF_MEMORY;
        /* ignore empty data */
      else {
        data->req.location = location;
        if(data->set.http_follow_location) {
          data->req.newurl = strdup(data->req.location); /* clone */
            return CURLE_OUT_OF_MEMORY;
          /* some cases of POST and PUT etc needs to rewind the data
             stream at this point */
          result = http_perhapsrewind(conn);
            return result;

    The location is followed with a status code between 300 and 399

    解决 无用
    打赏 举报

相关推荐 更多相似问题