Git Product home page Git Product logo

Comments (1)

ebiron avatar ebiron commented on August 21, 2024

classı aşağıdaki gibi güncelleyiniz :
`<?php
/**

namespace Buki;

use GuzzleHttp\Client;
use Exception;

class AnadoluAgency implements AnadoluAgencyInterface
{
/**
* The client object.
*
* @var \GuzzleHttp\Client
*/
protected $client = null;

/**
 * Auth information.
 *
 * @var array
 */
protected $auth = [];

/**
 * Headers information.
 *
 * @var null
 */
protected $headers = null;

/**
 * Response object.
 *
 * @var \GuzzleHttp\Psr7\Response
 */
protected $response = null;

/**
 * Files attributes in Response.
 *
 * @var null
 */
protected $attr = null;

/**
 * Create a new instance.
 *
 * @param  string  $user
 * @param  string  $pass
 * @param  float|int  $timeout
 * @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];
}

/**
 * Add headers information for request.
 *
 * @param  string  $key
 * @param  string  $value
 * @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;
}

/**
 * Add time interval information in request header.
 *
 * @param  string  $start
 * @param  string  $end
 * @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;
}

/**
 * Add filter informations in request header.
 *
 * @param  string  $type
 *     provider, category, priorty, package,
 *     type, language, search
 * @param  array  $value
 * @return  \Buki\AnadoluAgency
 */
public function filter($type, array $value)
{
	$value = implode(',', $value);
	if($type == "search")
		$this->addPostParam("search_string", $value);
	else
		$this->addPostParam("filter_" . $type, $value);
	

	return $this;
}

/**
 * Add limit informations in header.
 *
 * @param  int  $limit
 * @param  int  $offset
 * @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;
}


/**
 * API Discover request.
 *
 * @param  string  $lang
 * @return  json|\GuzzleHttp\Psr7\Response
 */
public function discover($lang = "tr_TR")
{
	return $this->get("/abone/discover/" . $lang);
}

/**
 * API Search request.
 *
 * @return  json|\GuzzleHttp\Psr7\Response
 */
public function search()
{
	return $this->post("/abone/search/");
}

/**
 * API Document request.
 *
 * @param  string  $id
 * @param  string  $type
 *     aa:text 	-> newsml29, newsml12
 *     aa:picture -> print, web, mobile
 *     aa:video	-> hd, sd, web, mobile
 * @return  json|\GuzzleHttp\Psr7\Response
 */
public function document($id, $type = "newsml29")
{
	return $this->get( ("/abone/document/" . $id . "/" . $type) );
}

/**
 * Save the files in Response.
 *
 * @param  string  $path
 * @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] ]
		);
	}
}

/**
 * Get response content.
 *
 * @return  string|void
 */
public function getContent()
{
	if(!is_null($this->attr))
		return $this->attr["data"];
}

/*
 * Get response object for request
 *
 * @return  \GuzzleHttp\Psr7\Response
 */
public function getResponse()
{
	return $this->response;
}

/**
 * Get request for API.
 *
 * @param  string  $url
 * @return  json|\GuzzleHttp\Psr7\Response
 */
protected function get($url)
{
	return $this->request("GET", $url);
}

/**
 * Post request for API.
 *
 * @param  string  $url
 * @return  json|\GuzzleHttp\Psr7\Response
 */
protected function post($url)
{
return $this->request("POST", $url, ['form_params' => $this->postParams]);
}

/**
 * API Request.
 *
 * @param  string  $method
 * @param  string  $url
 * @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;
}


/**
 * Time formatting.
 *
 * @param  string  $time
 * @return  string
 */
protected function timeFormat($time)
{
	$timestamp = strtotime($time);
	return sprintf(
		"%sT%sZ",
		date("Y-m-d", $timestamp), date("H:i:s", $timestamp)
	);
}

}
`

from aa-api.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.