REST service basic authentication with php - php

I am trying to use data in json format from REST service.
REST service requires Basic Authentication.
Here is the code in php:
$login = 'username';
$password = 'password!';
$url = 'https://host/rest/serviceLink';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
However, i cant get anything else from the service.

Related

PHP CURL Redirects

I have a web application that I enter an IP Address, username and password into, I then click submit. The page submits this data and redirects to a done page.
The done page finalises the update.
I have to login to access this page and my issue seems to be when I get redirected to the done page my login credentials are not being passed across.
This is the code I'm using :
$ch = curl_init();
$url = "http://127.0.0.1/test/test.cgi?ip=127.0.0.1&user=USERNAME&password=PASSWORD&submit=Update";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$LOGIN:$PASSWORD");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$result=curl_exec($ch);
var_dump($result);
Can anyone advise if I'm missing something or why this may not work ?
Thanks
This works for me:
<?php
$login = 'login';
$password = 'password';
$url = 'http://your.url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
echo($result);

MailGun Get Unsubscribes

I am using PHP's CURL function to return all unsubscribed emails in my domain using MailGun. I tried the following code:
function get_unsubscriptions() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-[key removed]');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/[domain removed]/unsubscriptions');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
But it only returns the following:
Mailgun Magnificent API
What am I doing wrong?

authenticate through curl and get resource [duplicate]

I am having problem with PHP curl request with basic authorization.
Here is the command line curl:
curl -H "Accept: application/product+xml" "https://{id}:{api_key}#api.domain.com/products?limit=1&offset=0"
I have tried by setting curl header in following ways but it's not working
Authorization: Basic id:api_key
or
Authorization: Basic {id}:{api_key}
I get the response "authentication parameter in the request are missing or invalid" but I have used proper id and api_key which is working in command line curl (I tested)
Please help me.
Try the following code :
$username='ABC';
$password='XYZ';
$URL='<URL>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
Can you try this,
$ch = curl_init($url);
...
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
...
REF: http://php.net/manual/en/function.curl-setopt.php
$headers = array(
'Authorization: Basic '. base64_encode($username.':'.$password),
);
...
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Its Simple Way To Pass Header
function get_data($url) {
$ch = curl_init();
$timeout = 5;
$username = 'c4f727b9646045e58508b20ac08229e6'; // Put Username
$password = ''; // Put Password
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); // Add This Line
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = "https://storage.scrapinghub.com/items/397187/2/127";
$data = get_data($url);
echo '<pre>';`print_r($data_json);`die; // For Print Value
Check My JSON Value

Calling elasticsearch via curl in php

I have some problems getting data from elasticsearch. I have tested the following simple php script (no framework necessary):
<?php
$url = "curl -XGET http://<myelasticurl>:80/_cat/indices";
$username = "testUser";
$password = "secret";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($output);
?>
The only response I get is: "bool(false)"

How to use basic authorization in PHP curl

I am having problem with PHP curl request with basic authorization.
Here is the command line curl:
curl -H "Accept: application/product+xml" "https://{id}:{api_key}#api.domain.com/products?limit=1&offset=0"
I have tried by setting curl header in following ways but it's not working
Authorization: Basic id:api_key
or
Authorization: Basic {id}:{api_key}
I get the response "authentication parameter in the request are missing or invalid" but I have used proper id and api_key which is working in command line curl (I tested)
Please help me.
Try the following code :
$username='ABC';
$password='XYZ';
$URL='<URL>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
Can you try this,
$ch = curl_init($url);
...
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
...
REF: http://php.net/manual/en/function.curl-setopt.php
$headers = array(
'Authorization: Basic '. base64_encode($username.':'.$password),
);
...
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Its Simple Way To Pass Header
function get_data($url) {
$ch = curl_init();
$timeout = 5;
$username = 'c4f727b9646045e58508b20ac08229e6'; // Put Username
$password = ''; // Put Password
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); // Add This Line
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = "https://storage.scrapinghub.com/items/397187/2/127";
$data = get_data($url);
echo '<pre>';`print_r($data_json);`die; // For Print Value
Check My JSON Value

Categories