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.
Related
I'm trying to realize a simple authorization using login and password. I get a cookie but not sure if authorized or not (how to check?), also I am trying to redirect after authorization but as a result, I get Error 404 after my code tried to redirect (followlocation, true).
Here's my php code
Trying just to redirect but 404 occurred.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);
echo $result;
curl_close($curl);
And here's trying to authorize, not sure if it works (but I get cookie and in header I get 200 OK)
$loginData = [
urlencode('j_username') => 'someName',
urlencode('j_password') => 'somePassword'
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($loginData));
curl_setopt($curl, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/authCookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'].'/authCookie.txt');
$result = curl_exec($curl);
echo $result;
curl_close($curl);
Also, I use urlencode but not sure if I use it in the right way (I have it in curl like $curl --location --request POST 'http://someFullName' \ --data-urlencode 'j_username=someUsername' \ --data-urlencode 'j_password=somePassword')
for auth you can use http basic auth it very simple in use for client and server, client code in this case will be look like:
<?php
const COOKIE_FILE = '/tmp/authCookie';
$host = 'localhost';
$username = 'login';
$password = 'pass';
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $username, $password));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
$return = curl_exec($ch);
curl_close($ch);
about your 4xx error, i think you have problems with server, for test redirections you can do something like:
<?php
//stable route with redirect
$host = 'https://bitly.is/EnterpriseButton';
$userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:76.0) Gecko/20100101 Firefox/76.0';
$curl = curl_init($host);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $host);
//with enabled followlocation
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
$curl = curl_init($host);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $host);
//with disabled followlocation
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
and for check auth work you can auth through browser with cookies from curl
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 am working in Opencart platform. now I am really stuck in integrating a sms api link. last few days I have done many things but I can't reach there.
I don't know what is the issue in my code. I am using CURL method:
function curl_get_contents($url)
{
$curl = curl_init();
if (substr(HTTPS_CATALOG, 0, 5) == 'https') {
curl_setopt($curl, CURLOPT_PORT, 443);
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
$json = curl_exec($curl);
if (!$json) {
$this->error['warning'] = sprintf($this->language->get('error_curl'), curl_error($curl), curl_errno($curl));
} else {
curl_close($curl);
}
}
this is my curl function, and I called the function like this.
$link= "http://**.**.**.**:****/sendsms/bulksms?username=****-****&password=********&type=0&dlr=1&destination=********&source=****&message=******"
$this->curl_get_contents($link);
I hope somebody will help me to survive from these problem
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?
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.