[英]Php cURL returns location.replace with a relative address, how to change?
I'm trying to login into a webpage using cUrl and this is the code: 我正在尝试使用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);
Running the script with the browser it returns a 404 Error but running the script with shell it returns "location.replace" containing a relative path (then executing the script on my server it can't find the location). 使用浏览器运行脚本会返回404错误,但是使用shell运行脚本会返回包含相对路径的“ location.replace”(然后在我的服务器上执行脚本时找不到位置)。
This is what cUrl returns: 这是cUrl返回的内容:
<html>
<head><script>location.replace("main.php?email=xxx%40xxx.xx&lang=it")</script></head>
<body>
<!--pre></pre-->
</body>
</html>
So, can I change the location? 那么,我可以更改位置吗? I've already tried to switch CURLOPT_FOLLOWLOCATION
to false but nothing changes. 我已经尝试将CURLOPT_FOLLOWLOCATION
切换为false,但是没有任何变化。
Can anyone help me? 谁能帮我?
Greetings 问候
CURLOPT_FOLLOWLOCATION
will just make curl to follow all redirects made on the server side. CURLOPT_FOLLOWLOCATION
只会使curl跟随服务器端进行的所有重定向。 It has no impact on the response string. 它对响应字符串没有影响。
I would make my login script return something more useful than html/JavaScript redirect. 我将使我的登录脚本返回比html / JavaScript重定向更有用的内容。 Consider doing something like this 考虑做这样的事情
loginscript.php 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;
And your curl-call 还有你的电话
<?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'];
}
If you can't change your login script for some reason then use regexp to match your response, something like this 如果您由于某种原因无法更改登录脚本,请使用regexp来匹配您的响应,如下所示
...
$sResponse = curl_exec($rCh);
preg_match('/location.replace\("(.+)"\)/', $sResponse, $aMatch);
header('Location:' . $aMatch[1]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.