无法使用 CURL 和 PHP 加载动态内容


Unable to load dynamic content using CURL and PHP

美好的一天,敬礼!我正在创建一个简单的网页来获取用户并从表单传递,并使用 CURL 将这些变量发布到另一个页面登录,然后显示结果返回(我将从这里开始,然后继续访问不同的页面)。问题是网站的某些内容是动态加载的(小部件)。因此,当我返回结果时,网站的框架加载,但小部件没有渲染。从第二个站点加载小部件时出错:"ptBaseURI = String(location).match(//ps(c ...'"这可能是会话问题吗?我的 php 代码:

$post_data['pwd']=$_REQUEST['password'];
foreach ($post_data as $key => $value){
$post_items[]=$key . '=' . $value;
}
$post_string = implode ('&',$post_items);
$tmp_file_name=tempnam("/tmp/","COOKIE");
$curl_handle=curl_init('www.targetsite.com/...');
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
//curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_handle, CURLOPT_COOKIEJAR,$tmp_file_name);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,true);
//curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($curl_handle);
$curl_handle=curl_init('www.targetsite.com/...');
curl_setopt($curl_handle, CURLOPT_COOKIEFILE,$tmp_file_name);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($curl_handle);
$cArray = curl_getinfo($curl_handle);
$newURL = $cArray[url];
echo str_replace('</head>','<base href="'.$newURL.'" /></head>',$result);

在结果上,您可以使用:$result = str_replace('', '', $result);

然后浏览器应该加载正确的页面,但删除登录cookie。

另一种方法是设置重写规则来获取请求,并转发请求使用 curl 获取登录 cookie 的通缉页面。