This repository was archived by the owner on May 8, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathSearch.php
More file actions
127 lines (111 loc) · 2.68 KB
/
Copy pathSearch.php
File metadata and controls
127 lines (111 loc) · 2.68 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* This file is part of the Yoozi Golem package.
*
* (c) Yoozi Inc. <hello@yoozi.cn>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Yoozi\LBS;
use Buzz\Browser;
use Buzz\Client\Curl;
use Buzz\Client\ClientInterface as HttpClientInterface;
use Illuminate\Support\Contracts\JsonableInterface;
use Illuminate\Support\Contracts\ArrayableInterface;
/**
* Yoozi LBS API toolkit using Baidu Map LBS WEB API.
*
* @author Saturn HU <yangg.hu@yoozi.cn>
*/
class Search implements ArrayableInterface, JsonableInterface
{
/**
* Search Query Object.
*
* @var \Yoozi\LBS\Query\QueryInterface
*/
protected $query;
/**
* Result array.
*
* @var array
*/
protected $items;
/**
* Setup ak code and inject the service object.
*
* @param string $ak
* @param \Yoozi\LBS\Query
*/
public function __construct($ak, $query)
{
$this->query = $query;
$this->query->set('ak', $ak);
}
/**
* Run the query via HTTP $client and fetch the result in $output format.
*
*
* @param \Buzz\Client\ClientInterface $client
* @return \Yoozi\LBS\Search
*/
public function run(HttpClientInterface $client = null)
{
$this->query->set('output', 'json');
$browser = new Browser($client ?: new Curl);
$response = $browser->get($this->query->url());
if ($response->isSuccessful()) {
$this->items = json_decode($response->getContent(), true);
if ($status = $this->items['status']) {
throw new \Exception("Service API return error: [Status $status].", 1);
}
}
return $this;
}
/**
* Set a URL segment OR query string using "dot" notation.
*
* <code>
* // Set the name of service.
* $this->set('name', 'place');
* </code>
*
* @param string $key
* @param string $val
* @return \Yoozi\LBS\Search
*/
public function set($key, $val)
{
$this->query->set($key, $val);
return $this;
}
/**
* Get the search query.
*
* @return \Yoozi\LBS\Query\QueryInterface
*/
public function getQuery()
{
return $this->query;
}
/**
* The search result array.
*
* @return array
*/
public function toArray()
{
return $this->items;
}
/**
* The search result in json.
*
* @param int $options
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->items, $options);
}
}