[英]Php cURL returns location.replace with a relative address, how to change?
我正在嘗試使用cUrl登錄到網頁,這是代碼:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
使用瀏覽器運行腳本會返回404錯誤,但是使用shell運行腳本會返回包含相對路徑的“ location.replace”(然后在我的服務器上執行腳本時找不到位置)。
這是cUrl返回的內容:
<html>
<head><script>location.replace("main.php?email=xxx%40xxx.xx&lang=it")</script></head>
<body>
<!--pre></pre-->
</body>
</html>
那么,我可以更改位置嗎? 我已經嘗試將CURLOPT_FOLLOWLOCATION
切換為false,但是沒有任何變化。
誰能幫我?
問候
CURLOPT_FOLLOWLOCATION
只會使curl跟隨服務器端進行的所有重定向。 它對響應字符串沒有影響。
我將使我的登錄腳本返回比html / JavaScript重定向更有用的內容。 考慮做這樣的事情
loginscript.php
$aReturn = array();
$bLoginSuccessfull = true;
if ($bLoginSuccessfull) {
$aReturn['url'] = 'main.php?email=xxx%40xxx.xx&lang=it';
} else {
$aReturn['error'] = 'something failed during login process';
}
$aReturn['success'] = $bLoginSuccessfull;
echo json_encode($aReturn);
exit;
還有你的電話
<?php
$sUrl = 'http://mysite.it/loginscript.php';
$rCh = curl_init();
curl_setopt($rCh, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($rCh, CURLOPT_URL, $sUrl);
curl_setopt($rCh, CURLOPT_POST, count($aFields));
curl_setopt($rCh, CURLOPT_POSTFIELDS, $sFields);
curl_setopt($rCh, CURLOPT_COOKIEFILE, $sCkfile);
curl_setopt($rCh, CURLOPT_HEADER, false);
curl_setopt($rCh, CURLOPT_RETURNTRANSFER, true);
$sResponse = curl_exec($rCh);
curl_close($rCh);
$sResponse = json_decode($sResponse);
if ($sResponse['success']) {
// Send user to url (main.php?email=xxx%40xxx.xx&lang=it)
header('Location: ' . $sResponse['url']);
} else {
echo "Login failed...";
echo $sResponse['error'];
}
如果您由於某種原因無法更改登錄腳本,請使用regexp來匹配您的響應,如下所示
...
$sResponse = curl_exec($rCh);
preg_match('/location.replace\("(.+)"\)/', $sResponse, $aMatch);
header('Location:' . $aMatch[1]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.