I'm trying to get the data returned from this link with CURL. However, I'm getting false as the output.
Here's my code and output:
Code:
$url = "https://airport.api.aero/airport/distance/LOS/ABV?user_key=MY_USER_KEY_GOES_HERE&units=mile";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
var_dump( curl_getinfo($ch) );
curl_close($ch);
var_dump($data);
Dump of curl_getinfo call
array (size=26)
'url' => string 'https://airport.api.aero/airport/distance/LOS/ABV?user_key=MY_USER_KEY&units=mile' (length=102)
'content_type' => null
'http_code' => int 0
'header_size' => int 0
'request_size' => int 0
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 0.702
'namelookup_time' => float 0.249
'connect_time' => float 0.468
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float -1
'starttransfer_time' => float 0
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string '107.22.197.175' (length=14)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 443
'local_ip' => string '192.168.1.17' (length=12)
'local_port' => int 53767
Dump of $data
boolean false
Is there something I'm doing wrong?
I was able to successfully send a get request (you should hide your key from your question lol)
Try this:
$url = "https://airport.api.aero/airport/distance/LOS/ABV";
$qry_str = "?units=mile&user_key=MY_USER_KEY_GOES_HERE";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = trim(curl_exec($ch));
curl_close($ch);
print $content;
Found the solution here. Since the url was using https, I needed to include this option curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);. That did it.
Are you able to make a curl request via command line?
i.e curl 'https://airport.api.aero/airport/distance/LOS/ABV?user_key=MY_USER_KEY_GOES_HERE&units=mile'
Related
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 used this code but I get always empty result I don't know why
if (isset($_POST['uname'], $_POST['password'])){
$uname = test_input($_POST['uname']);
$pw = test_input($_POST['password']);
$data = array(
"loginEmail" => $uname,
"loginPassword" => $pw,
"clientURI" => "https://hk2.notify.windows.com/?token=AwYAAAC2bng45wHDpXWajWLTxGM1gxP1DcLlHTL8%2bhOfKVbkpYjl1U9tnUGrW4FSmwuiiWPKEyvSUoO5v9nfzFWZ097kdUeN8xDpAlp96Ilk3LCSN0sQNFuyU%3d"
);
$url_send ="https://dev-frontserver.com/api/login";
$str_data = json_encode($data);
$headers = array("Content-Type: application/json");
function sendPostData($url, $post, $headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo "result = " .sendPostData($url_send, $str_data, $headers);*/
}
Any idea what is wrong with my code ? I tried the api in https://www.hurl.it and it work but I can not get it to work in my code.
Here are the returned data from `curl_getinfo($ch);'
array (size=26)
'url' => string 'https://dev-fronapp.com/api/login' (length=47)
'content_type' => string 'application/json; charset=utf-8' (length=31)
'http_code' => int 200
'header_size' => int 268
'request_size' => int 384
'filetime' => int -1
'ssl_verify_result' => int 20
'redirect_count' => int 0
'total_time' => float 2.14
'namelookup_time' => float 0
'connect_time' => float 0.344
'pretransfer_time' => float 1.047
'size_upload' => float 253
'size_download' => float 3991
'speed_download' => float 1864
'speed_upload' => float 118
'download_content_length' => float 3991
'upload_content_length' => float 253
'starttransfer_time' => float 2.125
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string '' (length=13)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 443
'local_ip' => string '' (length=12)
'local_port' => int 10484
Your modify code and changes made are
CURL SSL verifier is set to false
JSON error handling
API runtime exceptions
return values if api status is 1/200/ok/true.
<?php
if (isset($_POST['uname'], $_POST['password'])){
$uname = test_input($_POST['uname']);
$pw = test_input($_POST['password']);
$data = array(
"loginEmail" => $uname,
"loginPassword" => $pw,
"clientURI" => "https://hk2.notify.windows.com/?token=AwYAAAC2bng45wHDpXWajWLTxGM1gxP1DcLlHTL8%2bhOfKVbkpYjl1U9tnUGrW4FSmwuiiWPKEyvSUoO5v9nfzFWZ097kdUeN8xDpAlp96Ilk3LCSN0sQNFuyU%3d"
);
$url_send ="https://dev-frontserver.com/api/login";
$str_data = json_encode($data);
$headers = array("Content-Type: application/json");
echo "result = " .sendPostData($url_send, $str_data, $headers);
}
function sendPostData($url, $post, $headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
//avoid checking ssl verifier
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($ch);
if($result === false) {
throw new \RuntimeException(
'API call failed with cURL error: ' . curl_error($ch)
);
}
curl_close($ch);
$apiResponse = json_decode($result, true);
// Make sure we got back a well-formed JSON string and that there were no errors when decoding it
$jsonError = json_last_error();
if($jsonError !== JSON_ERROR_NONE) {
throw new \RuntimeException(
'API response not well-formed (json error code: ' . $jsonError . ')'
);
}
// Print out the response details
if($apiResponse['status'] === 1)//check field where
{
// No errors encountered
return $result;
}
else {
// An error occurred
die("Error:");//show error message from api
}
}
?>
Please test code and let me know if any modifications needed
Here is the command line command:
curl -H "Authorization: Bearer API_KEY" -X PUT https://graph.api.smartthings.com/api/smartapps/installations/DEVICE_ID/lock
Here is what i have uptill now, what is wrong with this code:
$headers = array('Authorization: Bearer ' . $st_api_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$response = curl_exec($ch);
curl_close($ch);
Output for var_dump(curl_getinfo($ch)); is, I am still unable to find the real error or i am doing something wrong in translating this code to PHP:
<?php array (size=26)
url => string 'https://graph.api.smartthings.com/api/smartapps/installations/c8137097-8532-43b8-b516-0573cb91ecee/setLockCode/20/3333' (length=118)
'content_type' => null
'http_code' => int 0
'header_size' => int 0
'request_size' => int 0
'filetime' => int -1
'ssl_verify_result' => int 1
'redirect_count' => int 0
'total_time' => float 0.547
'namelookup_time' => float 0
'connect_time' => float 0.282
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float -1
'starttransfer_time' => float 0
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string '54.243.113.196' (length=14)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 443
'local_ip' => string '192.168.0.139' (length=13)
'local_port' => int 62555
?>
After searching on internet i got this issue fixed by changing headers array and adding curl settings:
$headers = array('Authorization: Bearer ' . $st_api_token,
'Content-Type: application/json',
'Accept: json',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
I'm experiencing weird behaviour of a cURL request that I do in my PHP code.
I'm running the code locally on a standard WAMP Apache server.
Here's the code:
$auth_info = "...";
$some_url = "...";
$channel = curl_init();
curl_setopt($channel, CURLOPT_URL, $some_url);
curl_setopt($channel, CURLOPT_HTTPHEADER,
array("Authorization: BASIC " . base64_encode($auth_info))
);
curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
curl_setopt($channel, CURLOPT_HEADER, true);
$response = curl_exec($channel);
var_dump(curl_getinfo($channel));
$header_size = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
var_dump($header);
var_dump($body);
curl_close($channel);
If I'm executing this little PHP code snippet via my CLI (Powershell, since I'm running on Windows), everything is fine, all var_dumps work and I can see the $header and $body and everything and the data I expect is actually present.
Now for the weird behaviour. If I'm opening the script file with the above snippet in any browser, it just gives me:
array (size=26)
'url' => string 'the_url_here' (length=39)
'content_type' => null
'http_code' => int 0
'header_size' => int 0
'request_size' => int 0
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 1.234
'namelookup_time' => float 0
'connect_time' => float 0.109
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float -1
'starttransfer_time' => float 0
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string 'here_is_the_ip' (length=12)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 443
'local_ip' => string 'here_is_my_ip' (length=13)
'local_port' => int -> my_local_port_here
boolean false
boolean false
I'm completely puzzled since I cannot see a difference between the script beeing started by the CLI and beeing started by the browser. Has anyone got an idea on this?
EDIT Note: If I'm using Guzzle for the request, everything works fine, both in CLI and browser. I'm still interested in an answer why cURL is causing problems here.
Have you tried logging verbose output for the Curl request?
Normally I find this the best way to figure out what's going on under the hood...https://stackoverflow.com/a/14436877/682754
Also not as popular but this approach looks simple to implement and is a lot cleaner...https://stackoverflow.com/a/26611776/682754
This question is similar to: Freebase API call works in browser but fails with curl but no answers there.
I have this URL that works in the browser:
https://www.googleapis.com/freebase/v1/search?query=Cee+Lo+Green&filter=%28all+type%3A%2Fmusic%2Fartist+created%3A%22The+Lady+Killer%22%29&limit=10&key=my_key_here
Returns:
{"status":"200 OK","result":[{"mid":"/m/01w5jwb","id":"/en/cee_lo_1974","name":"Cee Lo Green","notable":{"name":"Singer-songwriter","id":"/m/016z4k"},"lang":"en","score":814.733643}],"cost":6,"hits":2}
However, in PHP, the following code doesn't work:
<?php
namespace com\voting;
class Freebase{
public function query(){
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => 'Cee Lo Green',
'filter' => '(all type:/music/artist created:"The Lady Killer")',
'limit' => 10,
'key' => API_KEY );
$url = $service_url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
}
?>
A dump of curl, gives this:
array (size=22)
'url' => string 'https://www.googleapis.com/freebase/v1/search?query=Cee+Lo+Green&filter=%28all+type%3A%2Fmusic%2Fartist+created%3A%22The+Lady+Killer%22%29&limit=10&key=my_key_here' (length=191)
'content_type' => null
'http_code' => int 0
'header_size' => int 0
'request_size' => int 0
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 0.047
'namelookup_time' => float 0
'connect_time' => float 0.063
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float -1
'starttransfer_time' => float 0
'redirect_time' => float 0
'certinfo' =>
array (size=0)
empty
'redirect_url' => string '' (length=0)
I thought maybe a connectivity issue, but I tried a regular http URL and curl showed that it was able to get the content of the page.
Update 1
I should have done my homework before posting. With Wireshark, I can see the following text:
Alert Message
Level: Fatal (2)
Description: Unknown CA (48)
Is my setup improperly configured?
Thanks, in just a script:
<?php
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => 'Cee Lo Green',
'filter' => '(all type:/music/artist created:"The Lady Killer")',
'limit' => 10,
'key' => API_KEY );
$url = $service_url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
?>
This answer solved it for me:
HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK
Final solution:
<?php
namespace com\voting;
class Freebase{
public function query(){
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => 'Cee Lo Green',
'filter' => '(all type:/music/artist created:"The Lady Killer")',
'limit' => 10,
'key' => API_KEY );
$url = $service_url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
}
?>