Exemple de còde PHP per trapar l'API REST del Congrès

La libraria php-curl deu èsser presenta dins lo vòstre environament d'execucion.

Verificacion de la libraria php-curl

dc@teslo:~$ php -r "phpinfo();"|grep curl
/etc/php5/cli/conf.d/20-curl.ini,
curl

Si que non i a besonh d'installar la libraria.
Jos Debian es possible d'installar la libraria amb la comanda apt-get install php5-curl, per exemple se la version de PHP es 5.

Exemple de requesta sus Verb'òc per lo vèrb cantar, lengadocian primièira persona del plural

Vaquí un tròç de còde PHP per anar querre l'informacion al dintre del site del Congrés de mercés son API REST. Primièr nos cal un objècte CurlClass qu'envelopa la comanda curl que fa partida de las librarias de PHP. Las comandas essencialas son :

  • curl_init,
  • curl_setopt,
  • curl_exec,
  • curl_close.

Un objècte a instanciar especializa l'objècte CurlClass per passar la requesta segon la bona sintaxi (l'URL e las comandas). Un còp bastit, demòra pas que tres linhas de còde per far virar la requesta.

$curl = new CurlCongres();
$curl->createCurl();
print_r($curl->__tostring()); 

Aquò's un còde d'ensaj, la resulta es un objècte JSON que se i pòt traire sas informacions.

<?php

include "CurlClass.php";

  class CurlCongres extends CurlClass{
    private $key = MA_CLEF;
    private $domains = ["expressoc.php","topoc.php","basic.php", "verboc.php","puntlenga.php", "synonyms.php", "rhymes.php"];
         public function __construct(){
       $s = "https://api.locongres.org/" . $this->domains[3] . "?key=". $this->key . "&inf=cantar&var=leng&per=1&num=pl";
       print ($s."\n");
       parent::__construct("https://api.locongres.org/" . $this->domains[3] . "?key=". $this->key . "&inf=cantar&var=leng&per=1&num=pl","");
     }
  } // MyCurl

$curl = new CurlCongres();
$curl->createCurl();
print_r($curl->__tostring()); 
?>

Resulta

La sortida es en UTF-8.
https://api.locongres.org/verboc.php?key=agadoodoodoopoozlananah&inf=cantar&var=leng&per=1&num=pl
{
        "query": [
                {
                        "form": "cantariam",
                        "id": 105852,
                        "per": "1",
                        "num": "pl",
                        "mod": "cond",
                        "tns": "pres",
                        "inf": "cantar"
                },
                {
                        "form": "cantem",
                        "id": 105856,
                        "per": "1",
                        "num": "pl",
                        "mod": "imp",
                        "tns": "pres",
                        "pol": "a",
                        "inf": "cantar"
                },
                {
                        "form": "cantem",
                        "id": 105859,
                        "per": "1",
                        "num": "pl",
                        "mod": "imp",
                        "tns": "pres",
                        "pol": "n",
                        "inf": "cantar"
                },
                {
                        "form": "cantarem",
                        "id": 105846,
                        "per": "1",
                        "num": "pl",
                        "mod": "ind",
                        "tns": "fut",
                        "inf": "cantar"
                },
                {
                        "form": "cant\u00e0vem",
                        "id": 105828,
                        "per": "1",
                        "num": "pl",
                        "mod": "ind",
                        "tns": "imp",
                        "inf": "cantar"
                },
                {
                        "form": "cant\u00e8rem",
                        "id": 105822,
                        "per": "1",
                        "num": "pl",
                        "mod": "ind",
                        "tns": "pas",
                        "inf": "cantar"
                },
                {
                        "form": "cantam",
                        "id": 105816,
                        "per": "1",
                        "num": "pl",
                        "mod": "ind",
                        "tns": "pres",
                        "inf": "cantar"
                },
                {
                        "form": "cant\u00e8ssem",
                        "id": 105840,
                        "per": "1",
                        "num": "pl",
                        "mod": "subj",
                        "tns": "imp",
                        "inf": "cantar"
                },
                {
                        "form": "cantem",
                        "id": 105834,
                        "per": "1",
                        "num": "pl",
                        "mod": "subj",
                        "tns": "pres",
                        "inf": "cantar"
                }
        ]
}

classa CurlClass

<?php
  class CurlClass {
     protected $_useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
     protected $_url;
     protected $_followlocation;
     protected $_timeout;
     protected $_maxRedirects;
     protected $_cookieFileLocation = './cookie.txt';
     protected $_post;
     protected $_postFields;
     protected $_referer ="http://www.google.com";

     protected $_session;
     protected $_webpage;
     protected $_includeHeader;
     protected $_noBody;
     protected $_status;
     protected $_binaryTransfer;
     public    $authentication = 0;
     public    $auth_name      = '';
     public    $auth_pass      = '';

     public function useAuth($use){
       $this->authentication = 0;
       if($use == true) $this->authentication = 1;
     }

     public function setName($name){
       $this->auth_name = $name;
     }
     public function setPass($pass){
       $this->auth_pass = $pass;
     }

     public function __construct($url,$followlocation = true,$timeOut = 30,$maxRedirecs = 4,$binaryTransfer = false,$includeHeader = false,$noBody = false)
     {
         $this->_url = $url;
         $this->_followlocation = $followlocation;
         $this->_timeout = $timeOut;
         $this->_maxRedirects = $maxRedirecs;
         $this->_noBody = $noBody;
         $this->_includeHeader = $includeHeader;
         $this->_binaryTransfer = $binaryTransfer;

         $this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt';

     }

     public function setReferer($referer){
       $this->_referer = $referer;
     }

     public function setCookieFileLocation($path)
     {
         $this->_cookieFileLocation = $path;
     }

     public function setPost ($postFields)
     {
        $this->_post = true;
        $this->_postFields = $postFields;
     }

     public function setUserAgent($userAgent)
     {
         $this->_useragent = $userAgent;
    }

     public function createCurl($url = 'nul')
     {
        if($url != 'nul'){
          $this->_url = $url;
        }

         $s = curl_init();

         curl_setopt($s,CURLOPT_URL,$this->_url);
         curl_setopt($s,CURLOPT_HTTPHEADER,array('Expect:'));
         curl_setopt($s,CURLOPT_TIMEOUT,$this->_timeout);
         curl_setopt($s,CURLOPT_MAXREDIRS,$this->_maxRedirects);
         curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
         curl_setopt($s,CURLOPT_FOLLOWLOCATION,$this->_followlocation);
         curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation);
         curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation);

         if($this->authentication == 1){
           curl_setopt($s, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
         }
         if($this->_post)
         {
             curl_setopt($s,CURLOPT_POST,true);
             curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);

         }

         if($this->_includeHeader)
         {
               curl_setopt($s,CURLOPT_HEADER,true);
         }         }

         if($this->_noBody)
         {
             curl_setopt($s,CURLOPT_NOBODY,true);
         }
         /*                                                                                                                                                                                                                                    
         if($this->_binary)                                                                                                                                                                                                                    
         {                                                                                                                                                                                                                                     
             curl_setopt($s,CURLOPT_BINARYTRANSFER,true);                                                                                                                                                                                      
         }                                                                                                                                                                                                                                     
         */
         curl_setopt($s,CURLOPT_USERAGENT,$this->_useragent);
         curl_setopt($s,CURLOPT_REFERER,$this->_referer);

         $this->_webpage = curl_exec($s);
                   $this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE);
         curl_close($s);

     }

   public function getHttpStatus()
   {
       return $this->_status;
   }

   public function __tostring(){
      return $this->_webpage;
   }
} // CurlClass

user/domenge/apicongres.txt · Dernière modification: 2019/03/25 21:15 par domenge
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0