【问题标题】:I am getting "Requested URL not found..." error with cURL. Any ideas?cURL 出现“未找到请求的 URL ...”错误。有任何想法吗?
【发布时间】:2013-06-27 22:49:46
【问题描述】:

所以我将数据发布到另一个系统,并希望检索系统响应的“重定向 url”,并在 2 秒后重定向用户,但我得到:

在此服务器上找不到请求的 URL /somescript.aspx。

知道我做错了什么吗?还是有别的什么在起作用?

<?php 
$strURL = 'http://someplace.com/somescript.aspx'; 
$fields_string = implode('&', (array)$_SESSION['apply']);

$resURL = curl_init(); 
curl_setopt($resURL, CURLOPT_URL, $strURL); 
curl_setopt($resURL, CURLOPT_POST, true);
curl_setopt($resURL, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($resURL, CURLOPT_HEADER, TRUE);
curl_setopt($resURL, CURLOPT_FOLLOWLOCATION, FALSE);
$response = curl_exec($resURL); 
$RedirectLocation = curl_getinfo($resURL, CURLINFO_EFFECTIVE_URL);

unset($_SESSION['apply']); ?>
<script type="text/javascript">
$(function() {
    setTimeout(function() {
        window.location.href = "<?php echo $RedirectLocation; ?>";
    }, 2000)
});
</script>

【问题讨论】:

    标签: php redirect curl


    【解决方案1】:

    所以您希望检索 301/302 重定向目标,但实际上并未遵循它?将CURLOPT_RETURNTRANSFER 设置为true,并从响应中解析Location: 标头。

    要了解我的意思,请将您的代码更改为:

    curl_setopt($resURL,CURLOPT_RETURNTRANSFER);
    $response = curl_exec($resURL); 
    echo $repsonse
    

    如果您不将 curl 设置为跟随重定向,IIRC CURLINFO_EFFECTIVE_URL 将不会改变。

    【讨论】:

    • 太棒了。我会试试的。但这会修复/纠正“请求的网址”错误吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多