Lucas Taulealea
Lucas Taulealea

Reputation: 395

Translating Ruby API call in PHP

I am trying to access an API, but the instructions I have been given are written for ruby, which I am not familiar with. I am coding in PHP and was after some assistance in translating the instructions:

GET /webservice/parties HTTP/1.1
Host: http://subdomain.domain.com
Authorization: Basic YXBpOmZhYTFmYmMwN2Q1MTczNTI3NGFj    

Additional instructions: username being sent should be “api”

Apologies if the question is rudimentary, unclear or already answered somewhere else. And thanks in advance.

Upvotes: 0

Views: 107

Answers (1)

bubba
bubba

Reputation: 3847

Try something like:

$username = 'api';
$password = 'YXBpOmZhYTFmYmMwN2Q1MTczNTI3NGFj';

$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://subdomain.domain.com/webservice/parties',
    CURLOPT_USERAGENT => 'Stack Overflow Example Agent',
    CURLOPT_USERPWD => $username . ":" . $password
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

You might also want to check How do I make a request using HTTP basic authentication with PHP curl?

I personally prefer to use the Guzzle Client Library which might look like:

use GuzzleHttp\Client;

$username = 'api';
$password = 'YXBpOmZhYTFmYmMwN2Q1MTczNTI3NGFj';

$client = new Client([
    'base_url' => ['http://subdomain.domain.com/webservice/parties'],
    'defaults' => [
        'auth'    => [$username, $password]
    ]
]);

Upvotes: 1

Related Questions