繁体   English   中英

php cURL返回带有相对地址的location.replace,如何更改?

[英]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.

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