Ian
Ian

Reputation: 379

PHP: get_headers issue with bitly AND long urls with spaces

I have a long URL:

$url='http://www.likecool.com/Car/Motorcycle/BMW%20Urban%20Racer%20Concept%20Motorcycle/BMW-Urban-Racer-Concept-Motorcycle.jpg';

I create a short one:

$url='http://goo.gl/oZ04P8';     
$url='http://bit.ly/1CzOQbf';

I run $headers = get_headers($url); print_r($headers);

SCENARIO:

get_headers works correctly for goo.gl short code but incorrectly for BITLY shortcode (404).

The difference is that BITLY shows up with spaces in the long url (bad) and GOOGL %20 (good).

When get_headers redirects the (long) url (with spaces) it FAILS.

I see no obvious way to fix this - am I missing something?

TWO OPTIONS - change the way BITLY encodes ? (I force %20 formatting in long url) - change the way get_headers encodes its URLs

Upvotes: 0

Views: 500

Answers (1)

Clément Malet
Clément Malet

Reputation: 5090

You could replace the content of the header by yourself once you received it :

$url = 'http://bit.ly/1CzOQbf';
$headers = get_headers($url, 1);
$headers['Location'] = str_replace(' ', '%20', $headers['Location']);
print_r($headers);

Output :

[Location]=>http://www.likecool.com/Car/Motorcycle/BMW%20Urban%20Racer%20Concept%20Motorcycle/BMW-Urban-Racer-Concept-Motorcycle-1.jpg

I added the second parameter of get_headers so it names the keys of the returned array, that way it's clearer to use / modify. It is obviously not needed at all.

Upvotes: 1

Related Questions