Reputation: 85
I am trying to consume a JSON API with PHP.
I have tried using CURL to get a response:
curl 'http://104.239.130.176:3000/api/users/authenticate?email=my_username_here&password=my_password'
This doesn't give me any response in the terminal.
I have also tried swapping email for username:
curl 'http://104.239.130.176:3000/api/users/authenticate?username=my_username_here&password=my_password'
I have written the following in a PHP file but this gives me a 404 error in the browser.
<?php
// Parameters
$tpm_base_url = 'http://104.239.176.130:3000/'; // ending with /
$req_uri = 'api/users/authenticate'; // GET /passwords.json
$username = 'my_username';
$password = 'my_password';
// Request headers
$headers = array(
'Content-Type: application/json; charset=utf-8'
);
// Request
$ch = curl_init($tpm_base_url . $req_uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // Includes the header in the output
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); // HTTP Basic Authentication
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Get headers and body
list($headers, $body) = explode("\r\n\r\n", $result, 2);
$arr_headers = explode("\r\n", $headers);
$arr_body = json_decode($body, TRUE);
// Show status and array of passwords
echo 'Status: ' . $status . '<br/>';
print_r($arr_body);
I am not sure where I am going wrong here. Instructions from the API developer are as follows:
The API has a basic JWT security so first request token.
POST request to: http://104.239.176.130:3000/api/users/authenticate
Upvotes: 1
Views: 1648
Reputation: 2401
try this
$ch = curl_init($tpm_base_url . $req_uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // Includes the header in the output
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"email=$username&password=$password");
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Upvotes: 0
Reputation: 581
curl -H "Accept: application/json" -H "Content-Type: application/json" --data "username=my_username_here&password=my_password" http://104.239.176.130:3000/api/users/authenticate
try this in terminal and see if you have any reponse
In php use CURLOPT_POSTFIELDS instead CURLOPT_USERPWD to send the user/pass
Upvotes: 1