Open artiweborg opened 4 years ago
classı aşağıdaki gibi güncelleyiniz : `<?php /**
namespace Buki;
use GuzzleHttp\Client; use Exception;
class AnadoluAgency implements AnadoluAgencyInterface { /**
@var \GuzzleHttp\Client */ protected $client = null;
/**
@var array */ protected $auth = [];
/**
@var null */ protected $headers = null;
/**
@var \GuzzleHttp\Psr7\Response */ protected $response = null;
/**
@var null */ protected $attr = null;
/**
@return void */ public function __construct($user, $pass, $timeout = 3.0) { $this->client = new Client([ // Base URI is used with relative requests 'base_uri' => 'https://api.aa.com.tr', // You can set any number of default request options. 'timeout' => $timeout, ]);
$this->auth = [$user, $pass];
}
/**
@return \Buki\AnadoluAgency */ public function addHeader($key, $value = null) { if(is_array($key)) foreach($key as $k => $v) $this->headers[$k] = $v; else $this->headers[$key] = $value;
return $this;
}
/**
@return \Buki\AnadoluAgency / public function time($start = '', $end = "NOW") { if($start != '*') $start = $this->timeFormat($start); if($end != "NOW") $end = $this->timeFormat($end);
$this->addHeader("start_date", $start);
$this->addHeader("end_date", $end);
return $this;
}
/**
@return \Buki\AnadoluAgency */ public function filter($type, array $value) { $value = implode(',', $value); if($type == "search") $this->addPostParam("searchstring", $value); else $this->addPostParam("filter" . $type, $value);
return $this;
}
/**
@return \Buki\AnadoluAgency */ public function limit($limit, $offset = null) { if(is_null($offset)) { $this->addPostParam("limit", $limit); $this->addPostParam("offset", 0); } else { $this->addPostParam("limit", $offset); $this->addPostParam("offset", $limit); }
return $this;
}
public function addPostParam($key, $value = null) { if (is_array($key)) { foreach ($key as $k => $v) { $this->postParams[$k] = $v; } } else { $this->postParams[$key] = $value; }
return $this;
}
/**
@return json|\GuzzleHttp\Psr7\Response */ public function discover($lang = "tr_TR") { return $this->get("/abone/discover/" . $lang); }
/**
@return json|\GuzzleHttp\Psr7\Response */ public function search() { return $this->post("/abone/search/"); }
/**
@return json|\GuzzleHttp\Psr7\Response */ public function document($id, $type = "newsml29") { return $this->get( ("/abone/document/" . $id . "/" . $type) ); }
/**
@return json|void */ public function save($path = null) { if(is_null($this->attr)) return;
if(is_null($path))
$path = getcwd();
$target = $path . "/" . $this->attr["filename"];
try
{
$file = fopen($target, 'w+');
if(!$file)
throw new Exception;
fwrite($file, $this->attr["data"]);
fclose($file);
return json_encode(
[ "response" => ["success" => true], "data" => ["file" => $target] ]
);
}
catch(Exception $e)
{
return json_encode(
[ "response" => ["success" => false] ]
);
}
}
/**
@return string|void */ public function getContent() { if(!is_null($this->attr)) return $this->attr["data"]; }
/*
@return \GuzzleHttp\Psr7\Response */ public function getResponse() { return $this->response; }
/**
@return json|\GuzzleHttp\Psr7\Response */ protected function get($url) { return $this->request("GET", $url); }
/**
@return json|\GuzzleHttp\Psr7\Response */ protected function post($url) { return $this->request("POST", $url, ['form_params' => $this->postParams]); }
/**
@return json|\GuzzleHttp\Psr7\Response */ protected function request($method, $url, $options = []) { $defaultOptions = [ 'auth' => $this->auth, 'headers' => $this->headers, ];
// Yalnızca POST isteklerinde Content-Type ekleyin ve $options doluysa ekleyin
if ($method === 'POST' && !empty($options)) {
$defaultOptions['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
}
// Kullanıcı tarafından sağlanan seçenekleri birleştirin
$options = array_merge($defaultOptions, $options);
// Guzzle istemcisine isteği gönderin
$this->response = $this->client->request($method, $url, $options);
$this->headers = null;
$this->attr = null;
// Yanıtı işleyin
if (stripos($this->response->getHeader("Content-Type")[0], "application/json", 0) === 0) {
return $this->response->getBody()->getContents();
} elseif ($this->response->hasHeader("Content-Disposition")) {
$disposition = $this->response->getHeader("Content-Disposition")[0];
$this->attr["filename"] = explode('=', $disposition)[1];
$this->attr["data"] = (string) $this->response->getBody();
return json_encode([
"response" => ["success" => true],
"data" => ["file" => $this->attr["filename"]]
]);
}
return $this->response;
}
/**
Selam,
parametreleri header olarak göndermişsiniz fakat çalışmıyor. form_params olarak gönderilmesi gerekiyor.