I have written below code and unable to get any curl response I have mentioned the debugging information below:
Can someone please help to find out why this issue curl_exec returning false.
$jwtToken = getToken();
$service_url = "https://example.com/api/Demo/GetProjectAttributes/1484";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array (
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Bearer $jwtToken"
) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/mycert.crt");
$response = curl_exec($ch);
if ($response === false ) {
$info = curl_getinfo($ch);
echo '<pre>';
die('error occured during curl exec. Additioanl info: ' . var_export($info));
curl_close($ch);
}
My curl is getting failed and I am getting below info:
array (
'url' => 'https://example.com/api/Demo/GetProjectAttributes/1484',
'content_type' => NULL,
'http_code' => 0,
'header_size' => 0,
'request_size' => 0,
'filetime' => -1,
'ssl_verify_result' => 0,
'redirect_count' => 0,
'total_time' => 0.57699999999999996,
'namelookup_time' => 0,
'connect_time' => 0.28100000000000003,
'pretransfer_time' => 0,
'size_upload' => 0,
'size_download' => 0,
'speed_download' => 0,
'speed_upload' => 0,
'download_content_length' => -1,
'upload_content_length' => -1,
'starttransfer_time' => 0,
'redirect_time' => 0,
'redirect_url' => '',
'primary_ip' => '104.209.135.21',
'certinfo' =>
array (
),
'primary_port' => 443,
'local_ip' => '192.168.1.4',
'local_port' => 56491,
)
PHP cURL extension provides functions that display the errors during the request, if it fails.
Try using them like that:
$response = curl_exec($ch);
if (!$response || curl_errno($ch) !== CURLE_OK)
{
echo 'cURL error (' . curl_errno($ch) . '): ' . curl_error($ch);
}
Related
<?php
$ch = curl_init();
//$concept_id = $_POST['concept_id'];
curl_setopt($ch, CURLOPT_URL, "https://browser.ihtsdotools.org/snowstorm/snomed-ct/browser/MAIN/2022-03-31/concepts/84114007");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded'));
$response = curl_exec($ch);
//echo curl_error($ch);
//echo '<p>';
//echo curl_errno($ch);
//echo '<p>';
$info = rawurldecode(var_export(curl_getinfo($ch),true));
curl_close($ch);
echo "<pre>\n$info<br>\n</pre>";
var_dump ($response);
?>
I am not able to retrieve the response of the SNOMED API. Works with my browser with the same URL.
I received no error message, just an empty response.
Tried to add these parameters without change :
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Here the response:
array (
'url' => 'https://browser.ihtsdotools.org/snowstorm/snomed-ct/browser/MAIN/2022-03-31/concepts/84114007',
'content_type' => NULL,
'http_code' => 423,
'header_size' => 112,
'request_size' => 123,
'filetime' => -1,
'ssl_verify_result' => 0,
'redirect_count' => 0,
'total_time' => 0.61424600000000007,
'namelookup_time' => 0.052975000000000001,
'connect_time' => 0.15305099999999999,
'pretransfer_time' => 0.51591100000000001,
'size_upload' => 0.0,
'size_download' => 0.0,
'speed_download' => 0.0,
'speed_upload' => 0.0,
'download_content_length' => 0.0,
'upload_content_length' => 0.0,
'starttransfer_time' => 0.61422100000000002,
'redirect_time' => 0.0,
'redirect_url' => '',
'primary_ip' => '3.225.65.37',
'certinfo' =>
array (
),
'primary_port' => 443,
'local_ip' => '10.102.1.146',
'local_port' => 41662,
)
string(0) ""
When we look for information on the 423 code returned, we see that sometimes pages are blocked because the client is not an Internet browser
So if we simulate an internet explorer with the "Agent" parameter, it works
Normally the code below should help you
// Determinate URL
$urlTest = "https://browser.ihtsdotools.org/snowstorm/snomed-ct/browser/MAIN/2022-03-31/concepts/84114007";
// Initialization Session CURL
$curlObject = #curl_init();
// Simulation of Agent
$config['useragent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
// Définition Options de la Session CURL
curl_setopt($curlObject, CURLOPT_USERAGENT, $config['useragent']);
curl_setopt($curlObject, CURLOPT_VERBOSE, false);
curl_setopt($curlObject, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlObject, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObject, CURLOPT_CONNECTTIMEOUT, 600);
curl_setopt($curlObject, CURLOPT_URL,$urlTest);
// Running Session CURL
$curlResponse = #curl_exec($curlObject);
// If successful execution
if($curlResponse!=false)
{
$dataList = json_decode($curlResponse, true); echo "<pre>"; print_r($dataList); echo "</pre>";
}
else echo "Error Connexion CURL - [".curl_errno($curlObject)."] ".curl_error($curlObject);
I dead stuck on an 401 response, another user suggested my header didn't included a leading white space for the API key field. I corrected it and am still receiving a 401. The API key should work, I paid for it last night, and regenerated a new one multiple times
my code:
<?php
$service_url = 'https://haveibeenpwned.com/api/v3/breachedaccount/?account=emailtest123#gmail.com';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$ch = curl_init ($service_url);
curl_setopt ($ch, CURLOPT_HTTPGET, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'hibp-api-key: 8bcxxxxxxxxxxxxxx1bc11',
'user-agent: FAUstudentproject'
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$returndata = curl_exec ($ch);
$status_code = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
print_r($status_code);
print_r($returndata);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
$json = curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded1 = json_decode($curl_response,true);
if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') {
echo "hello";
echo curl_getinfo($ch);
die('error occured: ' . $decoded1->response->errormessage);
}
echo 'response ok!';
print_r(json_decode($json));
var_export($decoded1->response);
?>
The response:
401{ "statusCode": 401, "message": "Access denied due to missing hibp-api-key." }array ( 'url' => 'https://haveibeenpwned.com/api/v3/breachedaccount/?account=emailtest123#gmail.com', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 20, 'redirect_count' => 0, 'total_time' => 0.04700000000000000011102230246251565404236316680908203125, 'namelookup_time' => 0.01600000000000000033306690738754696212708950042724609375, 'connect_time' => 0.0309999999999999997779553950749686919152736663818359375, 'pretransfer_time' => 0.0, 'size_upload' => 0.0, 'size_download' => 0.0, 'speed_download' => 0.0, 'speed_upload' => 0.0, 'download_content_length' => -1.0, 'upload_content_length' => -1.0, 'starttransfer_time' => 0.0, 'redirect_time' => 0.0, 'redirect_url' => '', 'primary_ip' => '2606:4700::6812:ad0d', 'certinfo' => array ( ), 'primary_port' => 443, 'local_ip' => '2601xxxxxxxxxxxxxxxxx21d3', 'local_port' => 59096, 'http_version' => 0, 'protocol' => 2, 'ssl_verifyresult' => 0, 'scheme' => 'HTTPS', )error occured during curl exec. Additioanl info:
You are using the wrong variable: replace curl_setopt($curl, CURLOPT_HTTPHEADER with curl_setopt($ch, CURLOPT_HTTPHEADER. You're using curl_init twice in your code.
Check your code to fix it, using only $ch or $curl, not both.
I have an authenticated key the I bought the the haveIbeenpwned API. I've been having trouble understanding the error i was getting until I was able to properly echo the response error i was getting.
I seriously doubt something could be wrong on the server's end and I'm convinced I must be at fault here.
<?php
$service_url = 'https://haveibeenpwned.com/api/v3/breachedaccount/?account=emailtest123#gmail.com';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$ch = curl_init ($service_url);
curl_setopt ($ch, CURLOPT_HTTPGET, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'hibp-api-key: 3b73aadxxxxxxxxxxxxxxa0c0bb4',
'user-agent: FAUstudentproject'
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$returndata = curl_exec ($ch);
$status_code = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
print_r($status_code);
print_r($returndata);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
$json = curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded1 = json_decode($curl_response,true);
if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') {
echo "hello";
echo curl_getinfo($ch);
die('error occured: ' . $decoded1->response->errormessage);
}
echo 'response ok!';
print_r(json_decode($json));
var_export($decoded1->response);
?>
My output:
401{ "statusCode": 401, "message": "Access denied due to missing hibp-api-key." }array ( 'url' => 'https://haveibeenpwned.com/api/v3/breachedaccount/?account=emailtest123#gmail.com', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 20, 'redirect_count' => 0, 'total_time' => 0.04700000000000000011102230246251565404236316680908203125, 'namelookup_time' => 0.01600000000000000033306690738754696212708950042724609375, 'connect_time' => 0.0309999999999999997779553950749686919152736663818359375, 'pretransfer_time' => 0.0, 'size_upload' => 0.0, 'size_download' => 0.0, 'speed_download' => 0.0, 'speed_upload' => 0.0, 'download_content_length' => -1.0, 'upload_content_length' => -1.0, 'starttransfer_time' => 0.0, 'redirect_time' => 0.0, 'redirect_url' => '', 'primary_ip' => '2606:4700::6812:ac0d', 'certinfo' => array ( ), 'primary_port' => 443, 'local_ip' => '2601xxxxxxxxxxxxxxxxx4670', 'local_port' => 61056, 'http_version' => 0, 'protocol' => 2, 'ssl_verifyresult' => 0, 'scheme' => 'HTTPS', )error occured during curl exec. Additioanl info:
From Specification
Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive. The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
with SP = space character, and LWS = Linear WhiteSpace
So your header hibp-api-key:3b73aadxxxxxxxxxxxxxxa0c0bb4 miss a space before the value.
I have the following code to get some information from a DataSnap server:
<?php
$username = USERNAME
$password = PASSWORD
$service_url = 'http://200.206.17.34:81/datasnap/rest/TServerMethods1/blogin/';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
$curl_response = curl_exec($curl);
echo $curl_response;
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('Error occured during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('Error occured: ' . $decoded->response->errormessage);
}
echo 'Response ok!';
var_export($decoded->response);
?>
What I get when I run this is:
array ( 'url' => 'http://200.206.17.34:81/datasnap/rest/TServerMethods1/blogin/', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 0, 'namelookup_time' => 0.0002249999999999999938417316602823348148376680910587310791015625, 'connect_time' => 0, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'redirect_url' => '', 'primary_ip' => '200.206.17.34', 'certinfo' => array ( ), )Error occured during curl exec. Additional info:
What am I doing wrong?
--EDIT:
I can connect to the address both with my browser and with wget.
The data from curl_info indicates a problem connecting to the server, e.g., "'content_type' => NULL, 'http_code' => 0", etc.
Try inserting print curl_error($curl) before closing the the $curl handler in the first if statement to get more information.
I'm trying to create connection between two system, one of them is main. The connection will be provided throught cURL. I have this function:
private function build_post($url, $params, $authentificate = true) {
//url-ify the data for the POST
$params_string= http_build_query($params);
$curl = curl_init($url);
//set the url, number of POST vars, POST data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_POST, count($params));
curl_setopt($curl,CURLOPT_POSTFIELDS, $params_string);
if ($authentificate) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'x-api-key: key1',
'x-api-skey: key2'
));
}
//execute post
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
echo('error occured during curl exec. Additioanl info: '.var_export($info));
}
else
{
echo 'Responze OK';
curl_close($curl);
}
}
From the system where I am trying to connect, I only have 2insctructions:
Each request to REST API must be authenticated by providing valid access & secret key of sender in request header.
for example:
x-api-key: D6d03y3h0iEW7Iz3xW9127a0xrh1ib
x-api-skey: sc1vcOTTFLuqjFa5u08UKtKaWl48XSqlm8jMQvrnXnuPvRjqTPgIDI6P1YcR
and second instruction is how should requested post look like.
I have no idea what is going wrong, all the time i get this:
array ( 'url' => 'https://rest.api.profitee.com/public/subscribe', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 1, 'redirect_count' => 0, 'total_time' => 0.037060000000000002662314813051125383935868740081787109375, 'namelookup_time' => 0.0003939999999999999817472395857720357525977306067943572998046875, 'connect_time' => 0.037070999999999999785504911642419756390154361724853515625, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'certinfo' => array ( ), 'primary_ip' => '54.229.78.229', 'redirect_url' => '', )error occured during curl exec. Additioanl info: NULL
Can someone help me make it work? Thanks
Add the following lines and see if it works.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);