1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MyPoseo API Bundle |
5
|
|
|
* |
6
|
|
|
* @author Tristan Bessoussa <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Tristanbes\MyPoseoBundle\Api; |
10
|
|
|
|
11
|
|
|
use Http\Client\Common\PluginClient; |
12
|
|
|
use Http\Client\HttpClient; |
13
|
|
|
use Doctrine\Common\Cache\Cache; |
14
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
15
|
|
|
use Http\Message\Authentication\QueryParam; |
16
|
|
|
use Http\Client\Common\Plugin\AuthenticationPlugin; |
17
|
|
|
|
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use Tristanbes\MyPoseoBundle\Connection\RestClient; |
21
|
|
|
use Tristanbes\MyPoseoBundle\Exception\NotEnoughCreditsException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @see http://fr.myposeo.com/nos-api/api-search/ |
25
|
|
|
*/ |
26
|
|
|
class Search implements SearchInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RestClient |
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param RestClient $client The http client |
35
|
|
|
*/ |
36
|
|
|
public function __construct(RestClient $client) |
37
|
|
|
{ |
38
|
|
|
$this->client = $client; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the identifiers of the search engine's extension |
43
|
|
|
* |
44
|
|
|
* @param string $searchEngine The search engine |
45
|
|
|
* @param integer $ttl The time to live for the cache |
46
|
|
|
* |
47
|
|
|
* @return array [['id' => 1, 'name => '.fr'] [...]] |
48
|
|
|
*/ |
49
|
|
|
public function getSearchEngineExtensions($searchEngine, $ttl = null) |
50
|
|
|
{ |
51
|
|
|
$cacheKey = sprintf('%s_locations', $searchEngine); |
52
|
|
|
|
53
|
|
|
$data = $this->client->get('tool/json', [ |
54
|
|
|
'method' => 'getLocations', |
55
|
|
|
'searchEngine' => $searchEngine, |
56
|
|
|
], $cacheKey, $ttl); |
|
|
|
|
57
|
|
|
|
58
|
|
|
return $data; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the town's code |
63
|
|
|
* |
64
|
|
|
* @param string $name The town name |
65
|
|
|
* @param string $country The country ISO |
66
|
|
|
* |
67
|
|
|
* @return array [['id' => 1, 'city_code => '1234', 'city_name' => 'dunkerque', 'code_dep' : '59']] |
68
|
|
|
*/ |
69
|
|
|
public function getTownCode($name, $country = 'FR') |
70
|
|
|
{ |
71
|
|
|
$data = $this->client->get('tool/json', [ |
72
|
|
|
'method' => 'getGeoloc', |
73
|
|
|
'country' => $country, |
74
|
|
|
'city' => $name, |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
return $data; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Retrieves the url position given a keyword |
82
|
|
|
* |
83
|
|
|
* @param string $keyword |
84
|
|
|
* @param string $url |
85
|
|
|
* @param string $searchEngine |
86
|
|
|
* @param string $callback |
87
|
|
|
* @param integer $geolocId |
88
|
|
|
* @param integer $location |
89
|
|
|
* @param integer $maxPage |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
* |
93
|
|
|
* { |
94
|
|
|
* "url_positioned": "", |
95
|
|
|
* "position": "+100", |
96
|
|
|
* "page": "-", |
97
|
|
|
* "type": "seo_natural", |
98
|
|
|
* "serp": "", |
99
|
|
|
* "nbr_results": 250000000, |
100
|
|
|
* "top": "https://urltestdefault.com/path/to/image", |
101
|
|
|
* "keyword": "keyword", |
102
|
|
|
* "url_search": "lemonde.fr", |
103
|
|
|
* "searchEngine": "google", |
104
|
|
|
* "location": "13" |
105
|
|
|
* } |
106
|
|
|
*/ |
107
|
|
|
public function getUrlRankByKeyword($keyword, $url, $searchEngine = 'google', $callback = null, $geolocId = null, $location = 13, $maxPage = null) |
108
|
|
|
{ |
109
|
|
|
$options = []; |
110
|
|
|
|
111
|
|
|
if ($callback) { |
|
|
|
|
112
|
|
|
$options['callback'] = $callback; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($geolocId) { |
|
|
|
|
116
|
|
|
$options['geolocId'] = $geolocId; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($maxPage) { |
|
|
|
|
120
|
|
|
$options['maxPage'] = $maxPage; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$data = $this->client->get('tool/json', array_merge([ |
124
|
|
|
'method' => 'getPosition', |
125
|
|
|
'keyword' => $keyword, |
126
|
|
|
'url' => $url, |
127
|
|
|
'searchEngine' => $searchEngine, |
128
|
|
|
'location' => $location, |
129
|
|
|
], $options)); |
130
|
|
|
|
131
|
|
|
return $data; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getNaturalSeoResult() |
135
|
|
|
{ |
136
|
|
|
// @todo, feel free to send a PR |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getSemResult() |
140
|
|
|
{ |
141
|
|
|
// @todo, feel free to send a PR |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.