簡體   English   中英

無法使用CURL和PHP加載動態內容

[英]Unable to load dynamic content using CURL and PHP

祝您有美好的一天! 我正在創建一個簡單的網頁來吸引用戶並從Form傳遞信息,然后使用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來獲取所需頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM