Code for capturing the search results of yahooboss through curl in php

Source: Internet
Author: User
Code for capturing the search results of yahooboss through curl in php 1. Compile the curl class to capture webpage content
The 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 object
The code is as follows:
$ CurlUtil = new CurlUtil ();

3. capture yahoo search results
The 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 = tzhion = 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 "));
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.