繁体   English   中英

PHP cURL 响应查询字符串

[英]PHP cURL Response query string

I am making a cURL request in PHP to a URL that immediately redirects back to the original page, appending a query string parameter on to the end of the redirect URL.

For example: On page load of https://url0.com , make a web request to https://url1.com

当请求https://url1.com时,它会重定向回https://url0.com?test=1

我的目标是从 cURL 请求中获取响应标头,然后读取查询字符串参数。 问题是响应标头总是如下所示:

HTTP/1.1 302 Found Cache-Control: private Content-Length: 153 Content-Type: text/html; charset=utf-8 Location: https://url0.com Server: Microsoft-IIS/8.5 Set-Cookie: ASP.NET_SessionId=0wjspsdi38dnclufiv3znpc; path=/; HttpOnly X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 22 Oct 2019 18:44:12 GMT

我不知道如何从中获取查询字符串。 我正在使用这些选项:

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

我不想要响应的正文,所以我使用CURLOPT_NOBODY来获取标题。

谢谢!

查看手册有CURLINFO_EFFECTIVE_URL curl_getinfo() function 的 CURLINFO_EFFECTIVE_URL。 像下面这样的东西可能会起作用:

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); //$lastUrl should contain the last URL called
curl_close($ch);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM