本文整理匯總了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) {
}
}