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);
Related
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 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);
}
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 store data exposed with the following URL in a PHP variable ($curl_response) for further manipulation, but my current code is not executing properly. I've copied/pasted the data exposed in the following URL into the body of an HTML file and have tried running the below script on that HTML file, and CURL works properly. I'm guessing the issue then has to do with getting a response correctly from this particular site. Perhaps there is a CURL option I'm overlooking.
http://tdm.prod.obanyc.com/api/pullouts/agency/MTABC/list
Thoughts?
<?php
$ch = curl_init("http://tdm.prod.obanyc.com/api/pullouts/agency/MTABC/list");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($ch);
if ($curl_response === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
else echo $curl_response ;
curl_close($ch);
?>
The debugging output is:
array ( 'url' => '', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 63.145232, 'namelookup_time' => 0.006015, '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, 'certinfo' => array ( ), 'primary_ip' => '10.137.36.11', 'primary_port' => 80, 'local_ip' => '', 'local_port' => 0, 'redirect_url' => '', )error occured during curl exec. Additioanl info:
Is your else statement in the question the same way it is in the code? If yes, then I would start by fixing the else statement
Change
else $curl_response ;
to
else
{
$curl_response; // You might want to echo $curl_response or store it or do something with it. In it's current state it just does nothing.
}
I was informed that the IP address of the site is blocked for servers not in-network. That resolves the no response error and null value of the variable upon CURL execution.
Thanks all