curl 迁移到 https 后返回 301 错误

curl returning 301 error after migrating to https

我们最近迁移到了 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:// 来避免重定向。

// conditions to send sms 

$data = array('username' => $username, 'apikey' => $apikey, 'numbers' => $mobile, "sender" => $sender, "message" => $msg);

if($this->sms_env == true)
{

    $ch = curl_init('https://api.textlocal.in/send?');

    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

    $response = curl_exec($ch);

    $decoded_response = json_decode($response, true);

    curl_close($ch);

    $sms_status = @$decoded_response['status'];

    $status['sms'] = $sms_status;
}

$db_data = array(
    'sms_response'  => @$response,
    'unique_code'   => @$unique_code,
    'mobile_no'     => @$mobile,
    'message'       => @$msg,
    'username'      => $username,
    'apikey'        => $apikey,
    'sender_id'     => $sender
);

return $db_data;