本文整理汇总了PHP中HttpClient::setUserAgent方法的典型用法代码示例。如果您正苦于以下问题:PHP HttpClient::setUserAgent方法的具体用法?PHP HttpClient::setUserAgent怎么用?PHP HttpClient::setUserAgent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpClient
的用法示例。
在下文中一共展示了HttpClient::setUserAgent方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: HttpClient
function _getMatches($word_list)
{
$xml = "";
// Setup HTTP Client
$client = new HttpClient('www.google.com');
$client->setUserAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR');
$client->setHandleRedirects(false);
$client->setDebug(false);
// Setup XML request
$xml .= '<?xml version="1.0" encoding="utf-8" ?>';
$xml .= '<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">';
$xml .= '<text>' . htmlentities($word_list) . '</text></spellrequest>';
// Execute HTTP Post to Google
if (!$client->post('/tbproxy/spell?lang=' . $this->lang, $xml)) {
$this->errorMsg[] = 'An error occurred: ' . $client->getError();
return array();
}
// Grab and parse content
$xml = $client->getContent();
preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\\/c>/', $xml, $matches, PREG_SET_ORDER);
return $matches;
}
示例2: array
<?php
include 'HttpClient.class.php';
//抓取页面的内容
$contents = HttpClient::quickGet('http://www.baidu.com/');
var_dump($contents);
//post请求某一个接口,返回的信息赋值给$res
$res = HttpClient::quickPost('http://example.com/sms.php', array('name' => 'kevin.liu', 'phone' => '18201042042'));
//可能有一些请求访问会出问题,则需要加一个userAgent
$client = new HttpClient('example.com');
$client->setDebug(true);
$client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207');
if (!$client->get('/')) {
die('An error occurred: ' . $client->getError());
}
$contents = $client->getContent();
//还有一些情况是:在采集数据的时候必须先登陆,则可以先模拟登陆
$client = new HttpClient('example.com');
$client->post('/login.php', array('username' => 'kevin', 'password' => '123456'));
if (!$client->get('/private.php')) {
//采集数据的目标地址
die('An error occurred: ' . $client->getError());
}
$pageContents = $client->getContent();
示例3: postRequest
private function postRequest($url, $cookies, $protocol)
{
$httpClient = new HttpClient($this->hostName);
$httpClient->setDebug(true);
try {
$httpClient->setUserAgent("PaiPai API Invoker/PHP " . PHP_VERSION);
if (!$httpClient->post($url, $this->getParams())) {
return '<p>Request failed!</p>';
} else {
return $httpClient->getContent();
}
} catch (Exception $e) {
}
}