【问题标题】:curl returning 301 error after migrating to httpscurl 迁移到 https 后返回 301 错误
【发布时间】:2018-10-17 14:55:52
【问题描述】:

我们最近迁移到 SSL,除了一项功能外,该网站运行良好。该函数在下面的代码中使用 curl 来执行位于同一服务器上的 api。 此函数的 url 变量是: news.hubsdev.com/administrator/index.php?option=com_api&task=acymailing.listcreate $ch 变量是-resource id='384' type='curl'

        $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
    curl_setopt($ch, CURLOPT_POST, count($data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

回复是

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://news.hubsdev.com/administrator/index.php?option=com_api&amp;task=acymailing.listcreate">here</a>.</p>
</body></html>

我们使用的是托管在 AWS 上的 PHP 5.6 版。我测试了 ssl 证书,它以“A”通过。

如何确定我收到此错误的原因?

谢谢! 肯

【问题讨论】:

  • 如果您添加curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 是否有效?另外,在网址中添加https://
  • 您被重定向到您网站的 HTTPS 版本
  • 换句话说,也许你的 $url 变量有 http 而不是 https
  • 我尝试了 CURLOPT_SSL_VERIFYPEER,并将 https:// 放入变量中 - 仍然是同样的错误

标签: php curl http-status-code-301


【解决方案1】:

您需要使用CURLOPT_FOLLOWLOCATION 选项跟随重定向:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

来自documentation

TRUE 以跟随服务器作为一部分发送的任何“Location:”标头 HTTP 标头(注意这是递归的,PHP 将遵循尽可能多的 "Location:" 发送的标头,除非 CURLOPT_MAXREDIRS 是 设置)。

或者您可以简单地在代码中使用https:// 来避免重定向。

【讨论】:

  • 谢谢,成功了!我假设没有潜在的意外后果,因为它们都在同一台服务器上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 2018-10-05
  • 2017-04-06
  • 2017-09-09
  • 2019-06-17
  • 2019-03-09
  • 2015-08-24
相关资源
最近更新 更多