在PHP file_get_contents中设置useragent



我正在尝试使用file_get_contents从页面获取内容以获取HTML和正则表达式以进行进一步处理。

网站我得到我的内容从有一个桌面和移动网站,所以我想知道是否有一种方法来发送一个自定义用户代理来获得移动网站而不是桌面网站?

使用file_get_contents我已经尝试了它与我的代码如下所示,但所有我得到的是一个空白页:

$options = array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: enrn" .
                  "Cookie: foo=barrn" .
                  "User-Agent: Mozilla/5.0 (iPad; U; CPU iPad OS 5_0_1 like Mac OS X; en-us)   AppleWebKit/535.1+ (KHTML like Gecko) Version/7.2.0.0 Safari/6533.18.5rn" // i.e. An iPad )
);
$context = stream_context_create($options);
$file = file_get_contents('http://www.example.com/api/'.$atrib,false,$context);
$doc = new DOMDocument();// create new dom document
$doc->loadHTML($file);// load the xmlpage 
$tags = $doc->getElementsByTagName('video'); // find the tag we are looking for
foreach ($tags as $tag) { // for ever tag that is the same make it a new tag of its own
    if (isset($_GET['key']) && $_GET['key'] == $key) { // if key is in url and matches script key - do or dont
    echo $tag->getAttribute('src'); // get out 3 min video from the attribute in page
    } else { // if key is not in url or not correct show error
        echo "ACCESS DENIED!"; // our out bound error
    }
}

我试图让用户代理从网站的移动页面加载内容,并使用正则表达式从该页中的这行代码中获取src url,以防这是问题:

<video id="player" src="http://example.com/api/4.m3u8" poster="http://example.com/default.png" autoplay="" autobuffer="" preload="" controls="" height="537" width="935"></video>

正如Casimir et Hippolyte在注释中提到的,取消注释"User-Agent: Mozilla...行末尾的右括号:

ini_set('display_errors', 'On');

最新更新