1. Write the Curl class, Carry on the webpage content crawl
Copy Code code as follows:
Class Curlutil
{
Private $curl;
Private $timeout = 10;
/**
* Initialize Curl Object
*/
Public Function __construct ()
{
$this->curl = Curl_init ();
curl_setopt ($this->curl, Curlopt_returntransfer, 1);
curl_setopt ($this->curl, curlopt_useragent, "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
curl_setopt ($this->curl, Curlopt_header, false); Set whether to display header information
curl_setopt ($this->curl, Curlopt_nobody, false); Set whether to output page content
curl_setopt ($this->curl, curlopt_connecttimeout, $this->timeout);
curl_setopt ($this->curl, curlopt_followlocation, true);
curl_setopt ($this->curl, Curlopt_autoreferer, true);
}
/**
* Logout function Closes Curl object
*/
Public Function __destruct ()
{
Curl_close ($this->curl);
}
/**
* Get the content of the Web page
*/
Public Function getwebpagecontent ($url)
{
curl_setopt ($this->curl, Curlopt_url, $url);
Return curl_exec ($this->curl);
}
}
2. Create a Curl Object
Copy Code code as follows:
$CurlUtil = new Curlutil ();
3. Crawl Yahoo search results
Copy Code code as follows:
function Getyahoosearch (Curlutil $curl, $key)
{
$key = UrlEncode ($key);
$SEARCHURL = "http://boss.yahooapis.com/ysearch/web/v1/$key? appid= your Yahoo appid&lang=tzh®ion=hk& Abstract=long&count=20&format=json&start=0&count=10 ";
$JOSNSTR = $curl->getwebpagecontent ($SEARCHURL);
$searchDataInfo = Json_decode ($josnStr, true);
$searchData = $searchDataInfo [' ysearchresponse '] [' resultset_web '];
$returnArray = Array ();
if (!empty ($searchData)) {
foreach ($searchData as $data) {
$returnArray [] = array ("url" => $data [' url '], "date" => $data [' Date '], ' title ' => strip_tags ($data [' title ']), ' d Escription ' => strip_tags ($data [' abstract ']));
}
}
return $returnArray;
}
4. Test results
Var_dump (Getyahoosearch ($CurlUtil, "Baidu"));