【问题标题】:How avoid Moved Permanently The document has moved here如何避免永久移动文档已移至此处
【发布时间】:2022-04-27 19:02:20
【问题描述】:

我在一个站点中,我会调用另一个站点中的 API。所以我建立了一个 curl

$url = ........
$curl_data = array('name'=>$name);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_data);

$output = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);

所以当我执行 curl 时,我会打印值“$output”并获得 Moved Permanently The document has move here。这是错误的,因为我会调用这个 api 我会获得价值并在进程开始时返回页面。谁能帮帮我?

一天后我解决在调用函数之前添加这一行:

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);

【问题讨论】:

    标签: php codeigniter curl


    【解决方案1】:

    检查 URL 是否确实抛出错误 301(永久移动)。使用 fiddler,因为它可以捕获 HTTP 错误代码。

    【讨论】:

    • 是的...但我不明白为什么它给了我这个 301
    • 网址是什么?你在使用重写引擎吗?
    【解决方案2】:

    看到这个PHP cURL says Moved Permanently when POSTing to a virtual host

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTREDIR, 3);
    

    【讨论】:

      【解决方案3】:

      添加这一行对我有用

      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      

      【讨论】:

        【解决方案4】:

        我也遇到过同样的问题,你可以通过将你的 url 前缀从 http 更改为 https 来解决它(如果是 http 则将其更改为 http)

        问题出在 .htaccess 中

        类似

        RewriteRule ............ [R=301,L]
        

        【讨论】:

          猜你喜欢
          • 2019-11-04
          • 2017-07-04
          • 2012-07-10
          • 1970-01-01
          • 2013-09-26
          • 2020-04-06
          • 2018-09-14
          • 2019-10-14
          • 2018-06-18
          相关资源
          最近更新 更多