Page 1 sur 1

Travailler avec un API

Posté : 30 janv. 2011, 21:38
par stefane321
Bonjour,

Pouvez-vous svp me dire le point de départ de travailler avec des API.

C'est que j'ai un projet de site web que j'intègrerais un outil de recherche de numéro de téléphone.

la compagnie national de téléphone fixe a un site web pour trouver les numéro de gens et ils fournissent un API pour mettre dans les sites web.

Cependant, je ne sais pas comment travailler avec ça.

Voici un exemple de code qu'ils fournissent;
<?php
/*
 * YellowAPI PHP API Library
 *
 * Requires: PHP HTTP extension (pecl_http)
 * Version: 0.1
 */


/**
 * YellowAPI class - used to make calls to the YellowAPI.
 */
class YellowAPI {


	/** Production API url */
	public static $PROD_URL = "http://api.yellowapi.com";
	/** Sandbox API url */
	public static $TEST_URL = "http://api.sandbox.yellowapi.com";

	/** 
	 * Api Key field 
	 * @access private
	 * @var string
	 */
	private $api_key;

	/** 
	 * Url used to perform calls 
	 * @access private
	 * @var string
	 */
	private $url;

	/** 
	 * The format of the response 
	 * @access private
	 * @var string
	 */
	private $format;
	

	function __construct($api_key, $test_mode=False, $format='XML') {
		$this->api_key = $api_key;
		$this->url = ($test_mode) ? self::$TEST_URL : self::$PROD_URL;
		$this->format = $format;
	}


	/**
	 * Find a business 
	 *
	 * @param string $what
	 *		the keyword or business name to search for
	 * @param string $where
	 *		the location to search
	 * @param string $uid
	 *		a unique identifier for the user of the application
	 * @param integer $page
	 *		the page of results to return
	 * @param integer $page_len
	 *		the number of results per page
	 * @param string sflag
	 *		the search flag to filter results
	 *
	 * @return string contents of the response as XML of JSON
	 */
	function find_business($what, $where, $uid, $page=NULL, $page_len=NULL,
			$sflag=NULL, $lang=NULL) {
		$url = $this->build_url('FindBusiness', array('what' => $what, 
				'where' => $where, 'UID' => $uid, 'pg' => $page,
				'pgLen' => $page_len, 'sflag' => $sflag,
				'lang' => $lang));
		return $this->perform_request($url);
	}

	/**
	 * Get details about a business
	 *
	 * @param string $prov
	 *		the province of the business
	 * @param string $business_name
	 *		the name of the business
	 * @param integer $listing_id
	 *		the listing id of the business
	 * @param stirng $uid
	 *		a unique identifier of the user of the application
	 * @param string $city
	 *		the city of the business
	 * @param string $lang
	 *		the language of the response
	 *
	 * @return string contents of the response as XML or JSON
	 */
	function get_business_details($prov, $business_name, $listing_id, $uid,
			$city=NULL, $lang=NULL) {
		$url = $this->build_url('GetBusinessDetails', array('prov' => $prov,
				'bus-name' => $business_name, 'listingId' => $listing_id,
				'UID' => $uid, 'city' => $city, 'lang' => $lang));
		return $this->perform_request($url);
	}

	/**
	 * Find dealers (sub business) of the parent business.
	 *
	 * @param integer $pid
	 * 		the listing id of the parent compant
	 * @param string $uid
	 * 		a unique identifier of the user of the application
	 * @param integer $page
	 *		the page of results to return
	 * @param integer $page_len
	 *		the number of results per page
	 * @param string $lang
	 *		the language of the response
	 *
	 * @return string the contents of the response as XML or JSON
	 */
	function find_dealer($pid, $uid, $page=NULL, $page_len=NULL, $lang=NULL) {
		$url = $this->build_url('FindDealer', array('pid' => $pid,
				'UID' => $uid, 'pg' => $page, 'pgLen' => $page_len,
				'lang' => $lang));
		return $this->perform_request($url);
	}


	/**
	 * Build a url from the parameter list.
	 *
	 * @param string $method
	 *		the API method name
	 * @param array $params
	 *		an associative array of param names to values
	 *
	 * @return string the fully constructed url
	 */
	private function build_url($method, $params) {
		$param_array = array();
		while (list($key, $value) = each($params)) {
			if (!isset($value)) {
				continue;
			}
			array_push($param_array, sprintf("%s=%s", $key, $value));
		}

		return sprintf("%s/%s/?%s&apikey=%s&fmt=%s", $this->url, $method,
				join("&", $param_array), $this->api_key, $this->format);
	}

	/**
	 * Perform the HTTP GET request.
	 *
	 * @param string $url
	 *		the url as a string
	 *
	 * @return string the contents of the response
	 */
	private function perform_request($url) {
		$request = new HttpRequest($url);
		$request->send();
		return $request->getResponsebody();
	}

}



?>

Quand j'exécute ce code directement sa me donne une page blanche:

Voir:
http://www.saglac411.com/testapi.php

Ils ont de la doc:

http://www.yellowapi.com/docs/read/Home

Je ne souhaite pas que vous fassiez le travail à ma place mais je souhaite que vous m'indiquiez le point de départ que je ne vois pas.

Merci

Re: Travailler avec un API

Posté : 30 janv. 2011, 22:17
par Invité
salut,
comme toute class , tu dois l'instancier.

Code : Tout sélectionner

$test = new YellowAPI();
on peut voir que la methode __construct() permet d' initialiser certaines proprietes.
donc

Code : Tout sélectionner

$test = new YellowAPI($apikey = 'la_cle_perso_pour_lapi',$test_mode = true, $format= 'XML);
puis tu as 3 methodes publiques qui te permettront de faire tes requetes:
find_business()
get_business_details()
find_dealer()

avec des arguments pour chaque, ainsi pour la premiere methode:

Code : Tout sélectionner

$biz = $test->find_business($what, $where, $uid, $page=NULL, $page_len=NULL,$sflag=NULL, $lang=NULL);
$biz contiendra alors le resultat de ta requete (ici format xml , tu as du json apparement aussi) que tu devras simplement parser!

++ :)

Re: Travailler avec un API

Posté : 29 sept. 2011, 11:51
par Yves1971
Bonjour.
Je souhaite savoir : l'API YellowAPI(); est - elle valable pour la recherche des annuaires en France [/14]?
Merci d'avance

Re: Travailler avec un API

Posté : 29 sept. 2011, 15:37
par stefane321
Bonjour.
Je souhaite savoir : l'API YellowAPI(); est - elle valable pour la recherche des annuaires en France [/14]?
Merci d'avance


non