Make an HTTP Request with POST Method to an external API for auth

Arif Rachman H Posted in Technical Support 5 years ago

Hello, I have a little bit confusing thing here :)

I want to make an POST call to an external API in login's action.
My first choice was Guzzle to help me to make HTTP Request call, but I got confused by how to install Guzzle in OSSN, like where should I call the > require 'vendor/autoload.php';
And how to use the GuzzleHTTPClient class properly in OSSN.
I hope you can show me a simple example of how to doing those things in OSSN.

Thanks

Replies
Indonesian Arif Rachman H Replied 5 years ago

Yes, I ended up using cUrl for doing that.
And it's already solved.
Thanks for answering : )

us Rishi B Replied 5 years ago

I have no idea what Guzzle is, but the standard way to do this in PHP is to use curl. You might find http://php.net/curl helpful. You don't need to do anything special to make use of curl functions in ossn. Here's some sample code which will send a few parameters by POST to a given url.

<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

curl_close ($ch);

// Further processing ...
if ($server_output == "OK") { ... } else { ... }
?>