Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents with spaces in URL

I have an issue where even if I replace the spaces to %20 and get this content the ultimate url the browser gets turns the "%20" into "%2520"

Here's my code, any suggestions to get this to work? it seems easy but I'm stuck :/

<?php
//$_GET['song'] will contain a song name with spaces
$song = str_replace(array("%20", "&", "?" , "/"), array(" ", "", "", ""), $_GET['song']);

// I use this to check how the GET 'song' looks after the str_replace
$list = "http://www.lyrdb.com/lookup.php?q=" . $song . "&for=fullt";
echo "list url is " . $list . "<hr>";

$content = file_get_contents("http://www.lyrdb.com/lookup.php?q=" . str_replace(" ", "%20", $song) . "&for=fullt");

echo $content;
?>

if you go to http://webservices.lyrdb.com/lookup.php?q=red%20hot%20chili%20peppers&for=fullt The result should output a list of lyric codes.

When i go to my website /?song=red hot chili peppers , it too converts spaces to %20 's but if it seems the browser converts the %'s to %25.

Can someone help me out?

like image 527
d-_-b Avatar asked Mar 07 '12 06:03

d-_-b


People also ask

What is the function file_get_contents () useful for?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string.

What is the difference between file_get_contents () function and file () function?

The file_get_contents() function reads a file into a string. The file_put_contents() function writes data to a file.

What will the file_get_contents () return?

The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

Does file_get_contents cache?

Short answer: No. file_get_contents is basically just a shortcut for fopen, fread, fclose etc - so I imagine opening a file pointer and freading it isn't cached.


1 Answers

$song = $_GET['song']);

$url = "http://www.lyrdb.com/lookup.php?for=fullt&q=";

echo "list url is " . htmlentities($url . $song) . "<hr>";

$content = file_get_contents($url . urlencode($song));

echo $content;
like image 130
Cheery Avatar answered Sep 21 '22 01:09

Cheery