php - 迁移到 https 后 curl 返回 301 错误

标签 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);

回应是
<!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”。

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

谢谢!

最佳答案

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

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

来自 documentation :

TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).



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

关于php - 迁移到 https 后 curl 返回 301 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50220342/

相关文章:

javascript - 使用 php/javascript 的 WP 自定义字段减慢了我的页面速度

php - 将 Ajax/UI JS 框架(ext、jquery-ui)与 MVC PHP 框架(zend、symfony)结合起来是个好主意吗?

php membersite_config.php 页面内部服务器错误

php - 发送 SOAP 1.2

php - 使用 HTML 表单或 cURL 在 PHP 中上传多个文件

apache - .htaccess 文件中哪个是正确/更好的规则?

.htaccess - 在 Google Blogger/Blogspot 中使用 301 重定向

php - 具有复杂 ORDER BY 的 MySQL 查询

php - Paypal 访问 - SSL 证书 : unable to get local issuer certificate

php - 如何使用 php 从 url 和 301 重定向获取变量?