I am trying to make a login session to a JSP file using PHP and Curl.
I am getting an error for invalid token
$data_string = json_encode($curl_post_data);
$service_url = 'http://zxczxczczxc.jsp';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "ZHJlZGR5:QnJzbm5yczY4Iw==");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json;charset ="utf-8"','Authorization: Basic xcxc:xcxc== Token zxcxzc='));
$curl_response = curl_exec($curl);
print_r($curl_response);
$result = json_decode($curl_response);
//print_r('Curl error: ' . curl_error($curl));
//echo '</pre>';
curl_close($curl);
Any ideas why I am getting this error?
Related
This the code.
$data = '{"rewardTime":"20","articleTime":"0","rewardType":"copper_treasure_chest","activeDay":"17","videoTime":"20","specific":"false","userid":"2944210","version":"3","day":"2020-05-07","token":"M2U4ZjQyMWItZmNiYi00NWM4LWJhYWYtOTZhZWEwY2ExODY5"}';
$url = 'https://api.cc.clipclaps.tv/reading/obtainReward';
$curl = curl_init();
curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$result = json_decode($result, true);
echo "$result"."\n";
This is the result:
{"code":4009,"msg":null,"data":null,"date":1588991702039}
It should be msg: success
You would need to refer to the API's documentation to figure out what Error: 4009 is exactly.
Your code (Curl request) is working, as it is receiving a proper JSON response.. and there is no coding error that I see.
Why the API is responding with an empty result/data field, is specific to the API itself.
why i am getting forbidden error my code is-
$api_key="my-api-key";
$json='{
"longUrl": "honortraders-2.myshopify.com/admin"
}';
$url="https://www.googleapis.com/urlshortener/v1/url?&key=".$api_key;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo $response;*i am trying to use google urlshortner api but i am getting forbidden error*
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.
Hi I'm trying to consume an API.
I need to add the API key in the header
API endpoint = http://overheid.io/api/kvk
if you want to test things, you can create an account here: https://overheid.io/auth/register
Documentation is in Dutch and can be found here: https://overheid.io/documentatie/kvk
This is what I came up with but do not get passed authentication.
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER => true);
curl_setopt($curl, CURLOPT_HTTPHEADER, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
?>
Edit: I've replaced the initial code part, with the solutions, but still no success.
Can anyone point me in the right direction?
Thanks!
You can try to use the CURLOPT_USERPWD option to pass the Authentication header. You also seemed to have some syntax errors.
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_USERPWD, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
But, this API seem to use a custom header...so you can try the following if it the above does not work:
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: yourkey"));
UPDATE:
I used the following with an account I created on their site:
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: key"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
This worked for me when I replaced the "key" part with the 64 character key they provide.
Trying to connect to a remote service, with the use of authorization (basic):
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_URL, 'https://online.moysklad.ru/exchange/xml/import');
curl_setopt($curl, CURLOPT_USERPWD, 'login:password');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'file' => '#'. ROOT .'/moysklad.xml',
'resolveByCode' => 'resolveByCode',
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo curl_error($curl);
exit();
}
curl_close($curl);
And always get 401 error. What's wrong?
HTTP 401 -> Unauthorized, you are using the wrong username or password.