1. Write a curl class to capture webpage content
Copy codeThe Code is as follows: class CurlUtil
{
Private $ curl;
Private $ timeout = 10;
/**
* Initialize a 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); // you can specify whether to display the header information.
Curl_setopt ($ this-> curl, CURLOPT_NOBODY, false); // you can specify whether to output the page content.
Curl_setopt ($ this-> curl, CURLOPT_CONNECTTIMEOUT, $ this-> timeout );
Curl_setopt ($ this-> curl, CURLOPT_FOLLOWLOCATION, true );
Curl_setopt ($ this-> curl, CURLOPT_AUTOREFERER, true );
}
/**
* Deregistering a function to close a curl object
*/
Public function _ destruct ()
{
Curl_close ($ this-> curl );
}
/**
* Obtain the webpage content
*/
Public function getWebPageContent ($ url)
{
Curl_setopt ($ this-> curl, CURLOPT_URL, $ url );
Return curl_exec ($ this-> curl );
}
}
2. Create a curl objectCopy codeThe Code is as follows: $ CurlUtil = new CurlUtil ();
3. Capture yahoo Search ResultsCopy codeThe Code is 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 & region = hk & abstract = long & count = 20 & format = json & start = 0 & count = 10 ";
$ JosnStr = $ curl-> getWebPageContent ($ searchUrl );
$ SearchDataInfo = json_decode ($ josnStr, true );
$ SearchData = $ searchDataInfo ['ysearchresponse'] ['resultedset _ web'];
$ ReturnArray = array ();
If (! Empty ($ searchData )){
Foreach ($ searchData as $ data ){
$ ReturnArray [] = array ("url" => $ data ['url'], "date" => $ data ['date'], 'title' => strip_tags ($ data ['title']), 'description' => strip_tags ($ data ['abstract ']);
}
}
Return $ returnArray;
}
4. Test Results
Var_dump (getYahooSearch ($ CurlUtil, "Baidu "));