PHP 使用bing搜索网站的api封装类用法

内容摘要
这篇文章主要为大家详细介绍了PHP 使用bing搜索网站的api封装类用法,具有一定的参考价值,可以用来参考一下。

对使用bing api搜索网站的php封装类对此感兴趣的朋友,看看idc笔
文章正文

这篇文章主要为大家详细介绍了PHP 使用bing搜索网站的api封装类用法,具有一定的参考价值,可以用来参考一下。

对使用bing api搜索网站的php封装类对此感兴趣的朋友,看看idc笔记做的技术笔记!这个类可以在网上搜索,使用了Bing搜索API。它可以发送HTTP请求到Bing搜索API的Web服务器执行搜索Web内容使用以前获得的API密钥。 类可以搜索网页,图片,视频,新闻和相关的关键字。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<code class="php">
<?
/**
 * 使用bing api搜索网站的php封装类
 *
 * @param
 * @author php教程 www.idcnote.com
 **/
class BingAPI{
    var $accountKey = '';
    var $ServiceRootURL 'https://api.datamarket.azure.com/Bing/Search/';
    var $WebSearchURL;
    var $searchText;
    var $searchType;
    var $request_data;
    var $AutoGet = true;
    var $ReturnType = 'JSON'; //Options: JSON, ARRAY
    var $ResultsLimit = 10;
    function __construct(){
    }
    function setQuery_Type($query,$type){
    $this->searchText = $query;
    switch($type){
        case 'Web':$this->searchType = 'Web';break;
        case 'Image':$this->searchType = 'Image';break;
        case 'News':$this->searchType = 'News';break;
        case 'Video':$this->searchType = 'Video';break;
        case 'Related':$this->searchType = 'RelatedSearch';break;
    }
    $this->createURL();
    }
    function createURL(){
    $this->WebSearchURL = $this->ServiceRootURL . $this->searchType .'?$format=json&$top='.$this->ResultsLimit.'&Query=';
    $this->context = stream_context_create(array(
        'http' => array(
        'request_fulluri' => true,
        'header'  => "Authorization: Basic " . base64_encode($this->accountKey . ":" . $this->accountKey)
        )
    ));
    $this->request = $this->WebSearchURL . urlencode( '\'' . $this->searchText . '\'');
    if($this->AutoGet){
        $this->get();
    }
    }
    function get(){
        $response = file_get_contents($this->request, 0, $this->context);
        $this->request_data = json_decode($response);
    }
    function decoded_data(){
        $r_array = array();
        switch($this->searchType){
        case 'Web':
            $obj = $this->request_data->d->results;
            $ic = count($obj);
            for($i=0;$i<$ic;$i++){
                $r_array[$i] = array('Title'=>$obj[$i]->Title,'Description'=>$obj[$i]->Description,'Url'=>$obj[$i]->Url);
            }
        break;
        case 'Image':
            $obj = $this->request_data->d->results;
            $ic = count($obj);
            for($i=0;$i<$ic;$i++){
                $r_array[$i] = array('Title'=>$obj[$i]->Title,'MediaURL'=>$obj[$i]->MediaUrl,'Width'=>$obj[$i]->Width,'Height'=>$obj[$i]->Height,'ContentType'=>$obj[$i]->ContentType,'Thumbnail'=>$obj[$i]->Thumbnail->MediaUrl);
            }
        break;
        case 'News':
            $obj = $this->request_data->d->results;
            $ic = count($obj);
            for($i=0;$i<$ic;$i++){
                $r_array[$i] = array('Title'=>$obj[$i]->Title,'Description'=>$obj[$i]->Description,'Url'=>$obj[$i]->Url,'Source'=>$obj[$i]->Source,'Date'=>$obj[$i]->Date);
            }
        break;
        case 'Video':
            $obj = $this->request_data->d->results;
            $ic = count($obj);
            for($i=0;$i<$ic;$i++){
                $r_array[$i] = array('Title'=>$obj[$i]->Title,'MediaUrl'=>$obj[$i]->MediaUrl,'DisplayUrl'=>$obj[$i]->DisplayUrl,'Runtime'=>$obj[$i]->Runtime,'Thumbnail'=>$obj[$i]->Thumbnail->MediaUrl);
            }
        break;
        case 'RelatedSearch':
            $obj = $this->request_data->d->results;
            $ic = count($obj);
            for($i=0;$i<$ic;$i++){
                $r_array[$i] = array('Keyword'=>$obj[$i]->Title);
            }
        break;
        }
        switch($this->ReturnType){
                case 'JSON':return json_encode($r_array);
                case 'ARRAY':return $r_array;
        }
    }
}
             
/***   来自php教程(www.idcnote.com)   ***/</code>

注:关于PHP 使用bing搜索网站的api封装类用法的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!