Code for capturing the search results of yahooboss through curl in php
Last Update:2017-05-13
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 "));