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
Related
I am trying to execute cURL in PHP to do a Stripe Subscription Search. But I am getting 400 Bad Request.
Here is my PHP script:
$ch = curl_init();
$created = 'created<' . strtotime('tomorrow');
$next_page=isset($_REQUEST['next_page'])==true?'page='.$_REQUEST['next_page']:'';
$limit = 20;
$curl_url = "https://api.stripe.com/v1/subscriptions/search?".$next_page."&expand[]=total_count&limit=".$limit."&query=status:'active' AND metadata['order_id']:'6735'".'AND '.$created;
//"https://api.stripe.com/v1/subscriptions/search?&expand[]=total_count&limit=20&query=status:'active' AND metadata['order_id']:'6735'AND created<1672272000"
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headr = array();
$headr[] = 'Stripe-Version: 2022-11-15';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'XXXX-secret_key_test-XXX');
$response = curl_exec($ch);
curl_close($ch);
I have solved the problem by encoding the query parameter values like
$curl_url = "https://api.stripe.com/v1/subscriptions/search?".$next_page."&expand[]=total_count&limit=".$limit
."&query=".urlencode($status.' AND '.$created)`
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
I have the following post api request, the example provided was using cURL command:
curl "https://muserver.com/api/gettoken" --request POST --include -
-header "Content-Type: application/json" --user test:test
I am trying to perform this request using cURL in PHP but getting authentication error.
following is what I have tried:
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);
I think I should pass the user and pass. in the form of
--user test:test
but really don't know how to do it.
You seem to be lacking the $username and $password variables :
$username='test';
$password='test';
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);
<?php
$url_simple_auth = 'https://www.some_url_need_basic_auth_first.com';
$url_download = 'https://www.some_url_need_basic_auth_first.com/dowonload_file.zip';
$username = 'some_login';
$password = 'some_pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_simple_auth);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// login simple auth
$simple_auth = curl_exec($ch);
// download file
curl_setopt($ch, CURLOPT_REFERER, $url_simple_auth);
curl_setopt($ch, CURLOPT_URL, $url_download);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "email=$username&key=$password");
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status_code);
var_dump($result);
I have tried the following code for login authentication but its not working.
<?php
define('URL', 'https://xxxxxxxxxxxx.com');
function authenticate($uname, $pass) {
$url = URL . 'Issue/Bug-5555';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$uname:$pass");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // ssl ensure cert
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); /// ssl ensure cert
$issue_list = (curl_exec($curl));
echo $issue_list;
return $issue_list;
} ?>
You didn't mention what you are trying to achieve with this code.
Are you trying to get a ticket info, post an issue? You are just using the API...
Well here's a script that works with the JIRA API.
<?php
$username = 'test';
$password = 'test';
$url = "https://xxxxx.xxxxxxx.net/rest/api/2/project";
$ch = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
$test = "This is the content of the custom field.";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}
curl_close($ch);
?>
This code is fetching a project from JIRA.
If you want to create issues, you will have to change the REST URL to /rest/api/2/issue/ and use "POST" instead of "GET" method.
You are missing a slash in the URL. The $url you pass to cURL currently has value:
https://jira.hawkdefense.comIssue/Bug-5555
After the curl_exec() line, insert this and copy/paste the result here.
if (curl_errno($url)) {
throw new Exception(curl_error($url));
}
I want to login using JIRA API with curl operation.
I having problem in curl operation. I have main URL in JIRA_URL.'/demo/111'; values for $username and $password are passed in the function correctly but shows the status 'failure'. Is any issues in my curl code
function JIRA_authenticate($username, $password) {
$url = JIRA_URL . '/demo/111';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // ssl ensure cert
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); /// ssl ensure cert
$issue_list = (curl_exec($curl));
echo $issue_list;
return $issue_list;
}
You didn't mention what you are trying to achieve with this code.
Are you trying to get a ticket info, post an issue? You are just using the API...
Well here's a script that works with the JIRA API.
<?php
$username = 'test';
$password = 'test';
$url = "https://xxxxx.xxxxxxx.net/rest/api/2/project";
$ch = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
$test = "This is the content of the custom field.";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}
curl_close($ch);
?>
This code is fetching a project from JIRA.
If you want to create issues, you will have to change the REST URL to /rest/api/2/issue/ and use "POST" instead of "GET" method.