gro*_*gel 42
如果您正在使用cURL,则可以使用它curl_setopt ($handle, CURLOPT_USERAGENT, 'browser description')来定义请求的用户代理标头.
如果您正在使用file_get_contents,请查看file_get_contents手册页上的示例调整:
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" .
"User-agent: BROWSER-DESCRIPTION-HERE\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
Run Code Online (Sandbox Code Playgroud)
如果要请求页面,请使用cURL。
为了设置标头(在本例User-Agent中为HTTP请求中的标头),您将使用以下语法:
<?php
$curl_h = curl_init('http://www.example.com/');
curl_setopt($curl_h, CURLOPT_HTTPHEADER,
array(
'User-Agent: NoBrowser v0.1 beta',
)
);
# do not output, but store to variable
curl_setopt($curl_h, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl_h);
Run Code Online (Sandbox Code Playgroud)