4

I have an API that I am trying to create a function for to send requests, the docs are here: http://simportal-api.azurewebsites.net/Help

I thought about creating this function in PHP:

function jola_api_request($url, $vars = array(), $type = 'POST') {
    $username = '***';
    $password = '***';

    $url = 'https://simportal-api.azurewebsites.net/api/v1/'.$url;

    if($type == 'GET') {
        $call_vars = '';
        if(!empty($vars)) {
            foreach($vars as $name => $val) {
                $call_vars.= $name.'='.urlencode($val).'&';
            }
            $url.= '?'.$call_vars;
        }
    }

    $ch = curl_init($url);

    // Specify the username and password using the CURLOPT_USERPWD option.
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  

    if($type == 'POST') {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    }

    // Tell cURL to return the output as a string instead
    // of dumping it to the browser.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //Execute the cURL request.
    $response = curl_exec($ch);

    // Check for errors.
    if(curl_errno($ch)){
        // If an error occured, throw an Exception.
        //throw new Exception(curl_error($ch));
        $obj = array('success' => false, 'errors' => curl_error($ch));
    } else {
        $response = json_decode($response);
        $obj = array('success' => true, 'response' => $response);
    }

    return $obj;
}

So this determintes whether its a GET or POST request, but the response being returned on some calls is that GET is not supported or POST is not supported, although I am specifying the correct one for each call.

I think I have the function wrong somehow though and wondered if someone could assist me in the right direction? As I've also noticed, I need to allow for DELETE requests too.

charlie
  • 415
  • 4
  • 35
  • 83

4 Answers4

9

for the easier life, try guzzle. http://docs.guzzlephp.org/en/stable/

you can make a request like this :

use GuzzleHttp\Client;
$client = new Client();
$myAPI = $client->request('GET', 'Your URL goes here');
$myData = json_decode($myAPI->getBody(), true); 

then you can access the data like an array

$myData["Head"][0]
5

The problem is in $url you try to create for GET request.

Your $url for GET request looks like:

GET https://simportal-api.azurewebsites.net/api/v1/?param1=val1&param2=val2

but from documentation you can clearly see that you $url should be:

GET https://simportal-api.azurewebsites.net/api/v1/param1/val1/param2

for ex.:

GET https://simportal-api.azurewebsites.net/api/v1/customers/{id}
Dmitry
  • 347
  • 1
  • 9
0

GuzzleHttp is the standard way to work with web service.

You can use auth parameter to send your authentication detail. Also, you can use Oath or Beer token whatever your convenient method is. If you try to call service via a token method, keep in mind you will need to pass authorization by header instead of auth.

See this GuzzleHttp authentication via token. Also, you can catch exception very quickly. See Handle Guzzle exception and get HTTP body

Try below code got from official site ;)

  $client = new GuzzleHttp\Client();
  $res = $client->request('GET', 'https://api.github.com/user', [
      'auth' => ['user', 'pass']
  ]);
  echo $res->getStatusCode();
  // "200"
  echo $res->getHeader('content-type')[0];
  // 'application/json; charset=utf8'
  echo $res->getBody();
  // {"type":"User"...'

  // Send an asynchronous request.
  $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
  $promise = $client->sendAsync($request)->then(function ($response) {
      echo 'I completed! ' . $response->getBody();
  });
  $promise->wait();

You can find more about GuzzleHttp request here: http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request

Hope this what you want!

Geee
  • 2,217
  • 15
  • 30
0

I think you should try to use Postman Tool to request to that API first. If postman does the job it means problem in your PHP code. But if you already used postman and still can't fetch response, so it may be problem with that API. Like URL block.

Sooraj Abbasi
  • 110
  • 16