【发布时间】:2018-05-27 06:41:32
【问题描述】:
我成功地对外部网站进行了一些 curl 调用,事实上,在最后一次调用之后,我能够看到我希望看到的网站页面。唯一的问题:我只能将此页面视为 html 响应,保留在本地主机中,并且网站页面不可导航。 这篇文章 (PHP cURL redirects to localhost) 讨论了一个类似的问题,但该解决方案似乎对我不起作用。
特别是,这是我的代码:
<?php
$products = explode(',', $_GET['products']);
$reqheaders = [
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding:gzip, deflate, br',
'Accept-Language:it',
'Connection:keep-alive',
'Host:www.clubpervoi.com',
'Referer:https://www.website.com/',
'Upgrade-Insecure-Requests:1',
'User-Agent:'. $_SERVER['HTTP_USER_AGENT']
];
$cookie_file = dirname(__FILE__) . '/cookie.txt';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.website.com/');
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIESESSION, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_exec($curl);
curl_close($curl);
// Action calls
for($i=0; $i<3; $i++) {
$ch = curl_init();
$params = array('code'=> $products[$i]);
$query = http_build_query($params);
try{
curl_setopt($ch, CURLOPT_URL, 'https://www.website.com/foo/addProduct?'.$query);
//curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $reqheaders);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$resp = curl_exec($ch);
curl_close($ch);
} catch (Exception $e) {
echo $e;
}
}
//Here I expect to be redirected to the summary page
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.website.com/foo/summary');
//curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $reqheaders);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_exec($ch);
curl_close($ch);
} catch (Exception $e) {
echo $e;
}
正如你在我设置的最后一次通话中看到的那样
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
理论上可以解决问题,但对我来说不是。
显然,如果我尝试使用一个
header('location https://www.website.com/foo/summary')
我得到了错误,而不是最后一次 curl 调用
Warning: Cannot modify header information - headers already sent by...
因为与之前的 curl_exec() 存在“冲突”,虽然我关闭了它
【问题讨论】: