The GET command gives two kinds of output, why?

iam using the GET command to get the page content. When I write the same command at a shell prompt, it gives the correct result, but when I use that in a PHP file, it sometimes gives the correct result, but sometimes it only gives half the content, that is, only a part and a half.

Iam using the following command in a shell script: -

GET http://www.abc.com/ -H "Referer:http://www.abcd.com/"

      

and following in PHP file: -

$data=exec('GET http://www.abc.com/ -H "Referer:http://www.abcd.com/"');
echo $data;

      

Now tell me why this command doesn't give the full content of the page when I use it in a php file.

+2


a source to share


3 answers


exec

returns only the last line from the command output file. To return full output, pass the second argument by reference:

exec('GET http://www.abc.com/ -H "Referer:http://www.abcd.com/"', &$data);



$data

will be an array with one element per line of output

+2


a source


It might be easier:

$data = `GET http://www.abc.com/ -H "Referer:http://www.abcd.com/"`;
echo $data;

      



Assuming the shell_exec function (what the downside `really is) is not disabled.

+1


a source


Alternative with pure php:


$opts = array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"Referer: http://www.abc.com/\r\n"
  )
);
$context = stream_context_create($opts);
$file = file_get_contents('http://www.abc.com/', false, $context);

      

0


a source







All Articles