function _loadVersion() { require_once JPATH_COMPONENT . DS . 'libraries' . DS . 'httpclient.class.php'; $client = new HttpClient('www.easy-joomla.org'); if (!$client->get('/components/com_versions/directinfo.php?catid=3')) { $this->setError($client->getError()); return false; } $this->_current = $client->getContent(); return true; }
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; }
<?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();