迁移到 https 后 curl 返回 301 错误

Ham*_*Hub 5 php curl http-status-code-301

我们最近迁移到 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);
Run Code Online (Sandbox Code Playgroud)

回应是

<!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>
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢!肯

Meh*_*nya 6

您需要使用以下CURLOPT_FOLLOWLOCATION选项遵循重定向:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
Run Code Online (Sandbox Code Playgroud)

文档

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

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