Anonymous
Проблема с запросом API на Skiddle: 301 перемещен.
Сообщение
Anonymous » 24 сен 2024, 18:45
Я пытаюсь запросить события через Skiddle API, используя конечную точку
http://www.skiddle.com/api/v1/events/search/ , но при попытке получаю следующий ответ:
Код: Выделить всё
HTTP/1.1 301 Moved Permanently
Server: CloudFront
Date: Wed, 19 Apr 2023 14:21:19 GMT
Content-Type: text/html
Content-Length: 167
Connection: keep-alive
Location: https://www.skiddle.com/api/v1/events/search/?api_key=xxxxxx&latitude=51.509865&longitude=-0.118092&radius=30&keyword=trance&limit=100&offset=0
X-Cache: Redirect from cloudfront
Via: 1.1 3fff5cbe8229c22a8e7cfe60a8827a1e.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: LHR61-P6
Alt-Svc: h3=":443"; ma=86400
X-Amz-Cf-Id: SLXpElnGSXFF0oD360hjXiwDF2sqfuHD5LggX96rDlUaoHGecskp1Q==
Вот мой код:
Код: Выделить всё
$skiddle = new Skiddle();
$rr = $skiddle->searchEvents('london','trance');
echo $rr->get();
class Locations
{
public static $london = [
'latitude'=>51.509865,
'longitude'=>-0.118092,
'radius'=>30
];
}
class Skiddle
{
const EVENTS_SEARCH_URL = 'http://www.skiddle.com/api/v1/events/search/';
const KEY = "xxxxxxx";
public function __construct(){}
public function searchEvents($location = "", $keyword = "")
{
$params = [
'api_key' => self::KEY
];
if ($location)
{
$params['latitude'] = Locations::$$location['latitude'];
$params['longitude'] = Locations::$$location['longitude'];
$params['radius'] = Locations::$$location['radius'];
}
if ($keyword)
{
$params['keyword'] = $keyword;
}
return new SkiddlePaginatedRequestResponse(
self::EVENTS_SEARCH_URL,
$params
);
}
}
class SkiddlePaginatedRequestResponse extends RequestResponse
{
protected $limit;
protected $offset;
public function __construct($url, $params = [], $limit = 100, $offset = 0)
{
parent::__construct($url, $params);
$this->limit = $limit;
$this->offset = $offset;
}
public function get()
{
return parent::get();
}
public function nextPage()
{
return new SkiddlePaginatedRequestResponse(
$this->url,
$this->params,
$this->limit,
$this->offset + $this->limit
);
}
protected function buildRequest()
{
parent::buildRequest();
$this->request['limit'] = $this->limit;
$this->request['offset'] = $this->offset;
}
protected function handleBadStatus($rawBody, $status, $headers)
{
$errorBody = json_decode($rawBody);
if (isset($errorBody->errormessage) && isset($errorBody->error)) {
// Error from the API
fault("Skiddle error", $errorBody->errormessage."\n".$errorBody->error);
die();
} else {
$this->dieHttpErrorFault($status, $headers);
}
}
}
class RequestResponse
{
protected $params;
protected $url;
protected $request;
protected $cookies = false;
public function __construct($url, $params = [])
{
$this->params = $params;
$this->url = $url;
}
public function setCookies($cookies)
{
//"mycookie1=value1; mycookie2=value2"
$this->cookies = $cookies;
}
public function get()
{
$this->buildRequest();
$url = $this->url . '?' . http_build_query($this->request);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => false
]);
if ($this->cookies) curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
$response = curl_exec($ch);
//$rawBody = curl_exec($ch);
if (curl_error($ch))
{
fault('cURL transport error', curl_errno($ch).'\n'.curl_error($ch));
die();
}
list($headers, $rawBody) = explode("\r\n\r\n", $response, 2);
$status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status < 200 || $status > 299) {
$this->handleBadStatus($rawBody, $status, $headers);
}
$object = json_decode($rawBody, true);
if (json_last_error() !== JSON_ERROR_NONE) {
fault('JSON parsing', "Error ".json_last_error_string()." (".json_last_error().")");
}
return $object;
}
protected function buildRequest()
{
$this->request = $this->params;
}
protected function handleBadStatus($rawBody, $status, $headers)
{
$this->dieHttpErrorFault($status, $headers);
}
protected function dieHttpErrorFault($status, $headers)
{
fault('HTTP error', "Code $status, \nHeaders:\n$headers");
die();
}
}
Пожалуйста, сообщите
Спасибо
Подробнее здесь:
https://stackoverflow.com/questions/760 ... -301-moved
1727192736
Anonymous
Я пытаюсь запросить события через Skiddle API, используя конечную точку http://www.skiddle.com/api/v1/events/search/, но при попытке получаю следующий ответ: [code]HTTP/1.1 301 Moved Permanently Server: CloudFront Date: Wed, 19 Apr 2023 14:21:19 GMT Content-Type: text/html Content-Length: 167 Connection: keep-alive Location: https://www.skiddle.com/api/v1/events/search/?api_key=xxxxxx&latitude=51.509865&longitude=-0.118092&radius=30&keyword=trance&limit=100&offset=0 X-Cache: Redirect from cloudfront Via: 1.1 3fff5cbe8229c22a8e7cfe60a8827a1e.cloudfront.net (CloudFront) X-Amz-Cf-Pop: LHR61-P6 Alt-Svc: h3=":443"; ma=86400 X-Amz-Cf-Id: SLXpElnGSXFF0oD360hjXiwDF2sqfuHD5LggX96rDlUaoHGecskp1Q== [/code] Вот мой код: [code]$skiddle = new Skiddle(); $rr = $skiddle->searchEvents('london','trance'); echo $rr->get(); class Locations { public static $london = [ 'latitude'=>51.509865, 'longitude'=>-0.118092, 'radius'=>30 ]; } class Skiddle { const EVENTS_SEARCH_URL = 'http://www.skiddle.com/api/v1/events/search/'; const KEY = "xxxxxxx"; public function __construct(){} public function searchEvents($location = "", $keyword = "") { $params = [ 'api_key' => self::KEY ]; if ($location) { $params['latitude'] = Locations::$$location['latitude']; $params['longitude'] = Locations::$$location['longitude']; $params['radius'] = Locations::$$location['radius']; } if ($keyword) { $params['keyword'] = $keyword; } return new SkiddlePaginatedRequestResponse( self::EVENTS_SEARCH_URL, $params ); } } class SkiddlePaginatedRequestResponse extends RequestResponse { protected $limit; protected $offset; public function __construct($url, $params = [], $limit = 100, $offset = 0) { parent::__construct($url, $params); $this->limit = $limit; $this->offset = $offset; } public function get() { return parent::get(); } public function nextPage() { return new SkiddlePaginatedRequestResponse( $this->url, $this->params, $this->limit, $this->offset + $this->limit ); } protected function buildRequest() { parent::buildRequest(); $this->request['limit'] = $this->limit; $this->request['offset'] = $this->offset; } protected function handleBadStatus($rawBody, $status, $headers) { $errorBody = json_decode($rawBody); if (isset($errorBody->errormessage) && isset($errorBody->error)) { // Error from the API fault("Skiddle error", $errorBody->errormessage."\n".$errorBody->error); die(); } else { $this->dieHttpErrorFault($status, $headers); } } } class RequestResponse { protected $params; protected $url; protected $request; protected $cookies = false; public function __construct($url, $params = []) { $this->params = $params; $this->url = $url; } public function setCookies($cookies) { //"mycookie1=value1; mycookie2=value2" $this->cookies = $cookies; } public function get() { $this->buildRequest(); $url = $this->url . '?' . http_build_query($this->request); $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $url, CURLOPT_SSL_VERIFYPEER => false ]); if ($this->cookies) curl_setopt($ch, CURLOPT_COOKIE, $this->cookies); $response = curl_exec($ch); //$rawBody = curl_exec($ch); if (curl_error($ch)) { fault('cURL transport error', curl_errno($ch).'\n'.curl_error($ch)); die(); } list($headers, $rawBody) = explode("\r\n\r\n", $response, 2); $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($status < 200 || $status > 299) { $this->handleBadStatus($rawBody, $status, $headers); } $object = json_decode($rawBody, true); if (json_last_error() !== JSON_ERROR_NONE) { fault('JSON parsing', "Error ".json_last_error_string()." (".json_last_error().")"); } return $object; } protected function buildRequest() { $this->request = $this->params; } protected function handleBadStatus($rawBody, $status, $headers) { $this->dieHttpErrorFault($status, $headers); } protected function dieHttpErrorFault($status, $headers) { fault('HTTP error', "Code $status, \nHeaders:\n$headers"); die(); } } [/code] Пожалуйста, сообщите Спасибо Подробнее здесь: [url]https://stackoverflow.com/questions/76085689/problem-making-api-request-on-skiddle-301-moved[/url]