0

Sorry if this is a duplicate question.
My target site redirects me to desktop site if the browser is not a mobile. I want to parse the mobile version of the site (http://mobile.mysite.com). I can't use Curl as my server is disabled for that. what would be the useragent for mobile if it is possible at all ??!!

Shawon Md
  • 195
  • 1
  • 2
  • 12

4 Answers4

2

If you need to send custom headers like User-Agent with your file_get_contents request, the PHP answer to that are stream contexts:

$opts = array(
    'http' => array(
        'method' => "GET",
        'header' => "Accept-language: en\r\n" .
                    "Cookie: foo=bar\r\n" .
                    "User-Agent: Foo Bar Baz\r\n"
    )
);

$context = stream_context_create($opts);

file_get_contents($url, false, $context);

See stream_context_create and file_get_contents.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

Pick a mobile User-Agent string and use it. They can easily be found from Google.

Here is some sample code that illustrates how to use them with file_get_contents():

<?php

   // The first one I found on Google
   $uaStr = 'Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91)';

   // Create a stream context
   // http://www.php.net/manual/en/context.http.php
   $context = stream_context_create(array(
     'http'=>array(
       'user_agent' => $uaStr
     )
   ));

   // The URL
   $url = "http://www.example.com/";

   // Make the request
   $result = file_get_contents($url, FALSE, $context);
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
0

try look this php libs:

PHP HttpClient

And if you want get mobile site, set the user agent to specific mobile browser

$userAgent = "NokiaC5-00/061.005 (SymbianOS/9.3; U; Series60/3.2 Mozilla/5.0; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) Version/3.0 Safari/525 3gpp-gba";
setUserAgent($userAgent);
Somy A
  • 1,682
  • 15
  • 18
0

To change your user agent within php without curl you may try this:

<?php
ini_set('user_agent', 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7');
$data = file_get_contents("http://www.mobile.example.com");
?>

PS: Got user agent of the iphone 4 from here !

Community
  • 1
  • 1
HamZa
  • 14,671
  • 11
  • 54
  • 75