cURL in not working in php - php

cURL is enabled in the server. But it's showing some error from the curl_exec($curl) point. No other error is showing from curl_error($curl);
Fatal error: Original Handler not found! in /public_html/curl_test.php on line 18
The cURL package is curl-7.29.0-46.el7.x86_64.
Could anyone suggest me any solution or can help me to troubleshoot the issue?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$curl = curl_init();
curl_setopt_array($curl, Array(
CURLOPT_URL => 'http://someurl.com',
CURLOPT_TIMEOUT => 120,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => 'UTF-8'
));
if(curl_exec($curl) === false)
{
echo 'Curl error: ' . curl_error($curl);
}
else
{
$data = curl_exec($curl);
echo 'Operation completed without any errors';
}
curl_close($curl);

The issue was in the zlib.output_compression.
It fixed and curl is working now.

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.

could not decode nested json api 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

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