你不能。
With curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl请按照响应LOCATION
仅限于3xx
状态码。
AFAIK并声明作为文件没有办法迫使卷曲跟随201位回应的位置。
您必须解析标头,获取LOCATION,然后发出第二个卷曲请求。
以下位置的状态不同于3xx
将是一个异常。此外,从curl命令行工具和C库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
给人一种快看卷曲源代码,你会发现在/lib/http.c
功能Curl_http_readwrite_headers
。
位置遵循的是里面有这个条件处理:
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);
if(!location)
return CURLE_OUT_OF_MEMORY;
if(!*location)
/* ignore empty data */
free(location);
else {
data->req.location = location;
if(data->set.http_follow_location) {
DEBUGASSERT(!data->req.newurl);
data->req.newurl = strdup(data->req.location); /* clone */
if(!data->req.newurl)
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);
if(result)
return result;
}
}
}
位置之后与300
和399
谢谢,我开始怀疑这一点。你能添加一个链接到描述这个的文档吗?我认为这会改善答案,我很乐意接受它。 – billynoah
http://php.net/manual/en/function.curl-setopt.php – Paolo
我删除了评论,还发布了一部分源代码,它提供了对文档的解释是正确的线索(状态没有重定向超出范围3xx) – Paolo