'curl returning 301 error after migrating to https

We recently migrated to SSL, and the site works great with the exception of one function. The function uses curl in the code below to execute an api located on the same server. The url variable for this function is: news.hubsdev.com/administrator/index.php?option=com_api&task=acymailing.listcreate The $ch variable is - 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);

The response is

<!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>

We are using PHP version 5.6 hosted on AWS. I tested the ssl certificate and it passed with an "A".

How can I determine why I am getting this error?

Thanks! Ken



Solution 1:[1]

You need to follow the redirect using the CURLOPT_FOLLOWLOCATION option:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

From the 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).

Or you can simply use https:// in your code to avoid the redirect.

Solution 2:[2]

// 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;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Spoody
Solution 2 Sonu Chohan