2012-07-12 09:21

如何使用php file_get_content解析移动网站


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 ( 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 ??!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongzang5815 dongzang5815 9年前

    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
    " .
                        "Cookie: foo=bar
    " .
                        "User-Agent: Foo Bar Baz
    $context = stream_context_create($opts);
    file_get_contents($url, false, $context);

    See stream_context_create and file_get_contents.

    点赞 评论 复制链接分享
  • duanhuan5409 duanhuan5409 9年前

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

    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("");

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

    点赞 评论 复制链接分享
  • dongtong1226 dongtong1226 9年前

    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";
    点赞 评论 复制链接分享
  • doukaizha5417 doukaizha5417 9年前

    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():

       // 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
       $context = stream_context_create(array(
           'user_agent' => $uaStr
       // The URL
       $url = "";
       // Make the request
       $result = file_get_contents($url, FALSE, $context);
    点赞 评论 复制链接分享