Code for capturing yahoo boss search results using curl in php

Source: Internet
Author: User

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 "));

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.