Implement code to crawl Yahoo boss search results under PHP with Curl

Source: Internet
Author: User
1. Write the Curl class to crawl Web content
Copy the 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);
}
/**
* Logoff function Close Curl Object
*/
Public Function __destruct ()
{
Curl_close ($this->curl);
}
/**
* Get the content of the webpage
*/
Public Function getwebpagecontent ($url)
{
curl_setopt ($this->curl, Curlopt_url, $url);
Return curl_exec ($this->curl);
}
}


2. Create a Curl Object
Copy the code code as follows:
$CurlUtil = new Curlutil ();

3. Crawl Yahoo search results
Copy the 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=tzhion=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" and "$data" [' Date '], ' title ' "= Strip_tags ($data [' title ']), ' d Escription ' = strip_tags ($data [' abstract ']);
}
}
return $returnArray;
}

4. Test results
Var_dump (Getyahoosearch ($CurlUtil, "Baidu"));
  • Related Article

    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.