PHP through the Curl crawl Yahoo boss Search results of the implementation code _php skills

Source: Internet
Author: User
Tags curl
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&region=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"));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.