Srikanth Rayabhagi
Srikanth Rayabhagi

Reputation: 1443

file_get_contents with url as variable

Can I use something like this to retrieve the page that is external with respect to the domain.

<?php
    $q_pass =$_REQUEST['query_passed'];
    $fetcher = "http://www.abc.com/search?q=".$q_pass;
 $homepage = file_get_contents($fetcher);
 echo $homepage;
?>

I have passed a variable and want to retrieve the result from the abc.com. Can $fetcher can be passed over to file_get_contents to retrieve the contents of the page? I am getting blank page when I hit this php page. But its again working fine if I use something like

 <?php
        $fetcher = "http://www.abc.com/search?q=query";
        $homepage = file_get_contents($fetcher);
        echo $homepage;
 ?>

What is happening here? Is there some technical explanation for this?

Upvotes: 4

Views: 2910

Answers (1)

Andries Mooij
Andries Mooij

Reputation: 153

You should use urlencode on $q_pass. I'm guessing your passing a value with spaces in it in query_passed.

Upvotes: 4

Related Questions