Duplosion
Duplosion

Reputation: 269

PHP - Curl is adding HTTP:// to my HTTPS URL

Here is my code that I'm using to troubleshoot an issue with my curl connection:

$url = 'https://myurl.com';
$encodedurl = urlencode($url);

$token = 'my_api_key';
$postfields = 'my data';

$_h = curl_init($newurl);
curl_setopt ($_h, CURLOPT_HTTPHEADER, array('Authentication: Token=mytoken'));
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_h, CURLOPT_POST, 1);
curl_setopt($_h, CURLOPT_URL, encodedurl);
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
curl_setopt ($_h, CURLOPT_POSTFIELDS, $postfields);

var_dump(curl_exec($_h));
var_dump(curl_getinfo($_h));
var_dump(curl_error($_h));

When I execute this, I receive this as part of the array:

["url"]=> string(59) "HTTP://https://myencodedurl.com/"

Anyone have any idea how to get rid of that HTTP:// it's adding to the beginning? I've tried str_replace at every possible place, but it seems to be happening only when curl_exec fires.

This is causing my app to fail, as the resource can't be reached...

Upvotes: 2

Views: 883

Answers (3)

lafor
lafor

Reputation: 12776

Urlencoded URL is not an URL anymore. It misses the protocol part, because :// gets percent-encoded (e.g. https%3A%2F%2Fmyurl.com) so curl assumes and adds http://. But http://https%3A%2F%2Fmyurl.com is still not a valid URL and the request fails.

Upvotes: 2

user2182349
user2182349

Reputation: 9782

Why are you using urlencode?

You can use:

$url = 'http://example.com';

And, as noted by @RhapX

curl_setopt($_h, CURLOPT_URL, encodedurl);

should be

curl_setopt($_h, CURLOPT_URL, $url);

Note variable name change.

Upvotes: 0

RhapX
RhapX

Reputation: 1683

Your CURLOPT_URL is not being set correctly. Make sure the variable you are passing it is $encodedurl and not encodedurl.

On top of that, you don't need to encode your URL. Thus, simply passing

curl_setopt($_h, CURLOPT_URL, $url);

would work just fine.

Upvotes: 0

Related Questions