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

时间:2012-07-12 09:21:54

标签: php mobile

很抱歉,如果这是一个重复的问题 如果浏览器不是移动设备,我的目标站点会将我重定向到桌面站点。我想解析该网站的移动版本(http://mobile.mysite.com)。我不能使用Curl,因为我的服务器被禁用了。 如果有可能的话,移动设备的用途是什么?!!

4 个答案:

答案 0 :(得分:2)

如果您需要使用User-Agent请求发送file_get_contents等自定义标头,那么PHP的答案是流上下文

$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);

请参阅stream_context_createfile_get_contents

答案 1 :(得分:0)

选择一个移动用户代理字符串并使用它。它们很容易found from Google

以下是一些示例代码,说明了如何将它们与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);

答案 2 :(得分:0)

试试看这个php libs:

PHP HttpClient

如果您想获得移动网站,请将用户代理设置为特定的移动浏览器

$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);

答案 3 :(得分:0)

要在php中更改您的用户代理而不卷曲,您可以尝试这样做:

<?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:从here获得了iphone 4的用户代理!