Php 在发送$\u POST请求后,获取响应头的内容处置
好的,要了解问题,首先请访问Php 在发送$\u POST请求后,获取响应头的内容处置,php,curl,content-disposition,Php,Curl,Content Disposition,好的,要了解问题,首先请访问 http://unblockproxy.nu/ 尝试浏览任何网站,比如()将其放在字段中,然后单击“取消阻止”按钮 发送$u POST请求后,站点应将您重定向到以下位置: http://unblockproxy.nu/index.php?x=Mfv0KjYRb3J3JO50MgBNbplFn2sTMoqPUIu1Unqn0bqdUoq5VbA9OnO8%3D 浏览器的响应标题如下所示: HTTP/1.1 302 Found Date: Fri, 06 M
http://unblockproxy.nu/
尝试浏览任何网站,比如()将其放在字段中,然后单击“取消阻止”按钮
发送$u POST请求后,站点应将您重定向到以下位置:
http://unblockproxy.nu/index.php?x=Mfv0KjYRb3J3JO50MgBNbplFn2sTMoqPUIu1Unqn0bqdUoq5VbA9OnO8%3D
浏览器的响应标题如下所示:
HTTP/1.1 302 Found
Date: Fri, 06 Mar 2015 12:49:30 GMT
Server: Apache/2.2.15
x-powered-by: PHP/5.3.3
Location: http://unblockproxy.nu/index.php?x=Mfv0KjYRb3J3JO50MgBNbplFn2sTMoqPUIu1Unqn0bqdUoq5VbA9OnO8%3D
Cache-Control: max-age=600, private, must-revalidate
Expires: Fri, 06 Mar 2015 12:59:30 GMT
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
HTTP/1.1 200 OK
Date: Fri, 06 Mar 2015 12:49:34 GMT
Server: Apache/2.2.15
X-Powered-By: PHP/5.3.3
Content-Disposition: inline; filename="samplepage.html"
Cache-Control: max-age=600, private, must-revalidate
Expires: Fri, 06 Mar 2015 12:59:34 GMT
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
这很简单,现在您可以使用此web代理获得浏览页面的内容
现在,我想用curl做同样的工作
我的问题是,我不知道如何让curl处理响应头的内容配置
下面是一些模拟我的问题的代码::
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://unblockproxy.nu/index.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('x' => 'http://www.example.com/samplepage.html'));
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
$string = curl_exec($ch);
curl_close($ch);
echo $string;
这将返回的内容,而这不是我想要的(浏览)
如果您想查看这个站点的脚本(仅2个PHP文件),您可以
谢谢。试试这个。如果我正确理解你的问题,这对我来说很好。我删除了很多没有任何作用的代码。事实证明,问题在于您没有在请求头中设置
referer
让我从头开始。通过POST
提交表单以使用代理查看给定网站后,会向发送请求。正如您在问题中提到的,index.php
处理表单提交并生成一个表单,它实际上只是将您重定向到另一个页面。假设您向index.php
发送了一个格式正确的请求,您可以解析响应头并获取重定向URL的值。按照下面的代码获取重定向URL
/**
* Submit the form via POST
* @param [site_url] The link to the page that you want to view
* eg: http://sitetoget.com/page.html
* @return A string containing the response headers
*/
function GetRedirect($site_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://unblockproxy.nu/index.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('x' => $site_url));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* Turn a header string into an associative array
* @param [response] The response headers from the form submission
* @return An array containing all of the headers
*/
function GetHeaders($response) {
$headers = [];
$text = substr($response, strpos($response, "\r\n\r\n"));
foreach(explode("\r\n", $text) as $i => $line) {
if($i === 0 || $i == 1) {
$headers['http_code'] = $line;
} else {
list($key, $value) = explode(': ', $line);
if($key != '' && $value != '') {
$headers[$key] = $value;
}
}
}
return $headers;
}
// Get the redirect URL
$redirect = GetRedirect('http://lancenewman.me/');
// Parse the response headers
$headers = GetHeaders($redirect);
// Save the redirect URL
$new_url = $headers['Location'];
现在您已经有了index.php
重定向到的URL,按如下方式向其发送cURL
请求。奇怪的是,我修补过的几乎所有其他请求头在确定该解决方案是否有效方面都没有起到任何作用。您的代码获取的内容而不是http://unblockproxy.nu
是因为您没有正确遵循重定向,并且没有在请求头中设置引用程序。Cookie、内容处置和所有其他标题似乎在解决这一问题中没有任何作用
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $new_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://unblockproxy.nu');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$string = curl_exec($ch);
curl_close($ch);
echo $string;
需要注意的是,某些页面上的一些图像、CSS和JS可能无法正确加载,因为有些页面使用相对URL而不是绝对URL。请记住这一点。问题是需要两次往返服务器才能完成请求。许多网站使用这种方法来减少“机器人”的数量或请求。第一个请求创建一个cookie(通常用于“会话”),该cookie必须存在才能处理表单
执行curl_exec()两次,看看是否得到想要的结果。第一次响应将发送cookie,curl将保存该cookie,因为您已启用cookie。第二次你应该会得到你想要的结果。检查我的更新答案。Cookie似乎与您的问题无关。它正在工作!非常感谢,HTTP Referer是关键,我使用Tamper Data addon跟踪了请求和响应头,我以前见过它,但我从未想过这是个问题,我正在为我的项目使用multi_curl,因此将CURLOPT_AUTOREFERER设置为TRUE对我来说是更好的选择,也不需要运行两次curl,只需设置REFERER,一切都会正常工作,再次感谢您的帮助:)根据我调试此的经验,内容配置与您的错误无关。请检查我更新的答案