Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request mobile version by using file_get_contents() in PHP?

Thanks for looking at my question.

I want to get the mobile version by the use of either file_get_contents() or cURL. I know that it can be done by the help of modifying the HTTP headers in the request. Can you please give me a simple example to do so?

Thanks again!

Regards, Sanket

like image 677
Sanket Sahu Avatar asked Feb 08 '11 18:02

Sanket Sahu


3 Answers

As an alternative, file_get_contents and stream_context_create can also be used:

$opts = array('http' =>
    array(
        'header'  => 'User-agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3',
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents($url, false, $context);
like image 129
igorw Avatar answered Nov 19 '22 03:11

igorw


Is this what you are looking for ?

curl -A "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3" http://example.com/your-url
like image 9
sfk Avatar answered Nov 19 '22 05:11

sfk


You need to set the user agent string:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
curl_close($ch);
like image 7
Alex Howansky Avatar answered Nov 19 '22 05:11

Alex Howansky