CodeIgniter Forums
[SOLVED]HTTP request API - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: [SOLVED]HTTP request API (/showthread.php?tid=89126)

Pages: 1 2


[SOLVED]HTTP request API - pippuccio76 - 01-12-2024

HI , im newbe for API , i want to send a request to a dispenser to know the product inside im .
In the explanation of the dispenser API every request must be with the autentication credential , send in the header of request .
To see list of product must call : 
https://www.dispenserweb.com/API/DispenserParsedAPI/GetProdotti
How can i implement a controller to do this request ?


RE: HTTP request API - InsiteFX - 01-12-2024

Your url link is not working.


RE: HTTP request API - pippuccio76 - 01-12-2024

(01-12-2024, 06:22 AM)InsiteFX Wrote: Your url link is not working.

Is an example the real url is  https://www.sinapsiweb.com/API/SinapsiParsedAPI/GetProdotti    ( i don't know if i can insert real url)


RE: HTTP request API - InsiteFX - 01-13-2024

Well it still does not work, can you give us a link to this API so that we can look at it?


RE: HTTP request API - pippuccio76 - 01-13-2024

(01-13-2024, 05:37 AM)InsiteFX Wrote: Well it still does not work, can you give us a link to this API so that we can look at it?

I have only a PDF , but i want know how can i request external data by passing credential in header . Can i do only with curl or must i  do in another way?


RE: HTTP request API - kenjis - 01-13-2024

See https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#headers


RE: HTTP request API - pippuccio76 - 01-15-2024

(01-13-2024, 05:40 PM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#headers




hi , this is my code :







Code:
  public function getDPIdispenser()
  {
   
    $uri = 'https://www.sinapsiweb.com/API/SinapsiParsedAPI/GetProdotti';




    $client = \Config\Services::curlrequest();

    $response = $client->request('GET', $uri, [
        'headers' => [
                        'Autorization' => 'Autorization:Basic Base64encoded(xxxxx:xxxxx)',

                    ],
    ]);


    $body = $response->getBody();

    dd($body);

  }




I have this error :   22 : The requested URL returned error: 401



In postman work only if  i work as this screenshot https://ibb.co/KFDGDn6



How can i do this with code ?

edit:


Solved by this code :





      $response = $client->request('GET', $uri , ['auth' => ['xxxxx', 'xxxxxx', 'basic']]);


RE: [SOLVED]HTTP request API - pippuccio76 - 01-16-2024

Another problem  with request :
TRY this:
Code:
  public function getTransazionidispenser()
  {
   
    $uri = 'https://www.sinapsiweb.com/API/SinapsiParsedAPI/GetTransazioni?dataDa=10/01/2024 00:00:00&dataA=15/01/2024 00:00:00';

    $client = \Config\Services::curlrequest();


    $response = $client->request('GET', $uri ,
                                                [
                                                  'auth' => ['xxx', 'xxx', 'basic']
                                                ],
                                               
                                );


    $body = $response->getBody();

    $res = json_decode($body);

    foreach($res as $v){
     
      echo '<pre>';
      print_r($v);
   
    }

  }
[/code]
with tis error :  
 
  CodeIgniter\HTTP\Exceptions\HTTPException
3 :

and this :
Code:
  public function getTransazionidispenser()
  {
   
    $uri = 'https://www.sinapsiweb.com/API/SinapsiParsedAPI/GetTransazioni';


    $client = \Config\Services::curlrequest();


    $response = $client->request('GET', $uri ,
                                                [
                                                  'auth' => ['xxx', 'xxx', 'basic']
                                                ],
                                                [
                                                  'query' =>  [
                                                                      'dataDa' => '10/01/2024 00:00:00',
                                                                      'dataA'  => '15/01/2024 00:00:00',
                                                                    ]
                                                ]

                                );


    $body = $response->getBody();

    $res = json_decode($body);

    foreach($res as $v){
     
      echo '<pre>';
      print_r($v);
   
    }

  }
   
with this error : 
CodeIgniter\HTTP\Exceptions\HTTPException

22 : The requested URL returned error: 500

in postman work fine :   https://ibb.co/GVvKPph

what can i do to pass parameter to query ?


RE: [SOLVED]HTTP request API - kenjis - 01-16-2024

See https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#query

and auth is also documented:
https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#auth


RE: [SOLVED]HTTP request API - pippuccio76 - 01-16-2024

(01-16-2024, 12:42 AM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#query

and auth is also documented:
https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#auth

is exactely the second code i have posted , but doesn't work , codeigniter return an incomprehensible error what mean   :

  CodeIgniter\HTTP\Exceptions\HTTPException
3 :

sorry with second code return 500...

this is the log :

Code:
CRITICAL - 2024-01-16 08:51:59 --> CodeIgniter\HTTP\Exceptions\HTTPException: 22 : The requested URL returned error: 500
in SYSTEMPATH\HTTP\CURLRequest.php on line 695.
1 SYSTEMPATH\HTTP\CURLRequest.php(695): CodeIgniter\HTTP\Exceptions\HTTPException::forCurlError('22', 'The requested URL returned error: 500')
2 SYSTEMPATH\HTTP\CURLRequest.php(387): CodeIgniter\HTTP\CURLRequest->sendRequest([...])
3 SYSTEMPATH\HTTP\CURLRequest.php(145): CodeIgniter\HTTP\CURLRequest->send('GET', 'https://www.sinapsiweb.com/API/SinapsiParsedAPI/GetTransazioni')
4 APPPATH\Controllers\AdminDPIRest.php(91): CodeIgniter\HTTP\CURLRequest->request('GET', 'https://www.sinapsiweb.com/API/SinapsiParsedAPI/GetTransazioni', [...], [...])
5 SYSTEMPATH\CodeIgniter.php(941): App\Controllers\AdminDPIRest->getTransazionidispenser()
6 SYSTEMPATH\CodeIgniter.php(502): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\AdminDPIRest))
7 SYSTEMPATH\CodeIgniter.php(361): CodeIgniter\CodeIgniter->handleRequest(null, Object(Config\Cache), false)
8 FCPATH\index.php(79): CodeIgniter\CodeIgniter->run()