简体   繁体   中英

Unable to load dynamic content using CURL and PHP

Good day and salutation! I am creating a simple web page to get a user and pass from a Form , and using CURL post these variables to another page to login, then show to result back ( I was gonna start from here then move on to accessing different pages). The problem is that some of the content of the site are loaded dynamically (Widgets). So when I get the result back the frame of the site loads up but the widgets are not rendering. Error on loading the widgets from the second site : "ptBaseURI = String(location).match(//ps(c ... " Could this be a session problem ? My php code:

$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);

on the result, you can use: $result = str_replace('', '', $result);

then the broser should load the correct pages, but whitout the login cookie.

an other way is to set up a rewrite rule to fetch thos requst, and forward the requests with curl to fetch the wanted page whit the login cookie.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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