could not decode nested json api php - php

I am using these code to fetch koinex api data. From this API URL -
https://koinex.in/api/ticker
<?php
$getCurrency = "inr";
$displayArrayOutput = true;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://koinex.in/api/ticker",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
if($displayArrayOutput){
$response = json_decode($response, true);
print_r($response);
}
else{
header("Content-type:application/json");
}
}
?>
I have also try file_get_contents but same problem. I have face this issue in 2 more api's. Note: Once i get the data and use it correctly but today this is not working again.

I tried your code, and apparently the website you are trying to reach with CURL uses a security:
Why have I been blocked?
This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.
What can I do to resolve this?
You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.
It seems that the website you are trying to reach asks for a user agent. This code works for me:
<?php
$getCurrency = "inr";
$displayArrayOutput = true;
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://koinex.in/api/ticker',
CURLOPT_USERAGENT => 'Something here'
));
// Send the request & save response to $resp
$response = curl_exec($curl);
// Close request to clear up some resources
$err = curl_error($curl);
curl_close($curl);
if ($err) {
} else {
if($displayArrayOutput){
$response = json_decode($response, true);
print_r($response);
}
else{
header("Content-type:application/json");
echo 'touine';
}
}
?>
Good luck

Related

How do I see the output from a PHP file that makes an API call?

I have a PHP file that triggers an API call when a user fills out a form on my site. At the end of the API call, it returns the response.
Where/how can I actually see this response? Filling out the form on the site runs the PHP file, but I'm not sure where it's actually outputting the response at.
Visting /myFile.php doesn't work because it's missing required inputs to actually run the API call.
Any help is much appreciated..
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.sendinblue.com/v3/smtp/email',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>json_encode($payload),
CURLOPT_HTTPHEADER => array(
'api-key: hidden',
'Content-Type: application/json',
'Cookie: __cfduid=dad4a689606c86c195e4d8ce33b53c5c51611684716'
),
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
The Sendinblue documentation says the HTTP status code of the response is 201 or 400.
You can use curl_getinfo() with CURLINFO_HTTP_CODE to get the status.
You can check if the messageId is defined in the response.
You also need to check to cURL errors using curl_error().
$response = curl_exec($curl);
// Check cURL error
if ($response === false) {
$error = curl_error($ch);
die("Error $error"); // for example
}
// decode JSON
$reponseData = json_decode($response, true);
// Check HTTP status
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// Status "Bad request"
if ($httpcode == 400) {
$error = $reponseData['message'];
die("Error $error"); // for example
}
// Status "Created"
if ($httpcode == 201) {
if (! empty($reponseData['messageId'])) {
// Message sent !
}
}

How to get data from a website that uses SSL using cURL and PHP?

I am trying to get some data from a website where you need to have a SSL certificate to be able to connect to it.
I have found the following code :
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $host);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $host);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
The cURL query works well, but the data that I get is this one :
400 No required SSL certificate was
sent 400 Bad
Request No required SSL certificate was
sent nginx
What I am looking for is the data contained in the index.php (and the other paths) of the website.
So, how can I do to add a Certificate to the code, and, using cURL, get the data from the website ?
PS : Will the data will be in JSON format ?
PPS : if this can be helpfull, I am using PHPStorm
Try this.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://jsonplaceholder.typicode.com/todos/1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array("content-type: application/json"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
can you try like this?
echo $str = file_get_contents("https://jsonplaceholder.typicode.com/todos/1");

cURL on proxied localhost returns Access Denied

I'm trying to access a .php file from another .php using cURL.
Both are on localhost and I'm using a proxy.
THE PROBLEM
If I try to access using postman or direct on the browser, the desired file returns correctly.
However, everytime that I try to access this file from another .php file using cURL, and passing all the proxy and auth values, it gives me the following:
Access Denied (authentication_failed)
Your credentials could not be authenticated: "Credentials are missing.". You will not be permitted access until your credentials can be verified.
This is typically caused by an incorrect username and/or password, but could also be caused by network problems.
For assistance, contact your network support team.
Her is my cURL:
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_PROXY => "proxy.myspecificproxy.com.br",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => "localhost/MyDesiredFile.php?param=test&mail=test%40test.com",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_PROXYUSERPWD => "myuser:mypassword123",
CURLOPT_PROXYPORT => "8080",
CURLOPT_PROXYTYPE => 'HTTP',
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Any ideas?
EDIT:
Beyond trying to access via postman or browser, if I cURL directly from the command line, it works too.
EDIT
Tried to disable the proxy for local addresses, but no change in the result.
Is there a redirect involved? Remember that cURL doesn't follow redirects unless you tell it to, by passing the --location flag.
Does it work if you run it from the command line? Try that, and try passing --verbose and --include to see the returned headers.
Update your code as below
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_PROXY => "proxy.myspecificproxy.com.br",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => "localhost/MyDesiredFile.php?param=test&mail=test%40test.com",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_PROXYUSERPWD => "myuser:mypassword123",
CURLOPT_PROXYPORT => "8080",
CURLOPT_PROXYTYPE => 'HTTP',
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
<?php
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => "http://localhost/",
CURLOPT_FOLLOWLOCATION => true,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Looks like it was a misconfiguration in the privileges.
I contacted the support team of the company and the problem was solved.

trouble connecting to Reed api - php

I cant seem to connect to the reed api, docs here: https://www.reed.co.uk/developers/jobseeker
it states that:
You will need to include your api key for all requests in a basic authentication http header as the username, leaving the password empty.
my code currently looks like this:
$api_key = 'MY-API-KEY';
$url = ' https://www.reed.co.uk/api/1.0/search?keywords=Accounts Assistant&resultsToTake=100&resultsToSkip=0'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, urlencode($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic '.$api_key
));
$server_output = curl_exec($ch);
curl_close($ch);
var_dump($server_output);
the output is always:
bool(false)
Is there something I have missed? Is anyone else able to connect?
I've tried the url directly in my browser and it works, after using my api key in the popup auth box so I know the url and api key is correct.
Please see this answer ( https://stackoverflow.com/a/13654911/5056954 )
I tried to access the reed website and it seems its also asking for a username. Basic auth is also base64 encoded, so the following snippet should work for you if the username is not required:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic '. base64_encode($api_key)
));
The easiest way is to use Postman. Once you tune up your query it gives you snippets of codes in many programming languages.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.reed.co.uk/api/1.0/search?keywords=accountant&location=london",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic YOUR-AUTOMATICALLY-ENCODED-API-KEY-BY-POSTMAN",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
The code from Postman includes (, "postman-token: xxxxxxx-xxxx-xxx-xx-xx"). Just delete that.
It's the space in the URL parameter.
It should be:
$url = 'https://www.reed.co.uk/api/1.0/search?keywords=Accounts%20Assistant&resultsToTake=100&resultsToSkip=0'

PHP Curl doesn't return a response

I'm trying to get the response back from an API using curl in php but for some reason it fails. However, when I open the API call link in a browser it works fine. Furthermore, if I try to get the response with curl after I've opened the link in the browser curl works but if I change the $url variable to a new link it again stops working until I open it in the browser again.
Here is my code, don't worry about the api key it's just a test:
<?php
set_time_limit(30);
$API_KEY = "ak_wujpBbfefrmxDleyAmnqtFpqAcmey";
$url = 'https://www.google.com/';
$api_call = "https://www.screenshot-website.com/api/$API_KEY?url=$url&type=tablet";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $api_call,
CURLOPT_TIMEOUT => 30,
CURLOPT_TIMEOUT_MS => 30000,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT_MS => 30000 )
);
$response = json_decode(curl_exec($curl), true);
if($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($curl); ?>
<img src="<?php echo $response['image']; ?>">
I get no errors at all.
I think your problem is with the "https" since curl set ssl_verifier to true
"The problem is that cURL has not been configured to trust the server’s HTTPS certificate." you can read the full information here.
You could try setting this option:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Categories