PhP cURL - 403 Error With API Key In Header - php

I'm relatively new to cURL in PHP, and I'm looking to cURL against an API where where the API Key is in the header. Here's what I have thus far:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://publicapi.ohgo.com/api/v1/reported-conditions?region=ne-ohio');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: APIKEY 016c348e-58fe-486f-ad72-9b2f41c55a56';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Unfortunately, after running this script, I get a 403 error printed to the screen after a few seconds:
Error:Failed connect to publicapi.ohgo.com:443; Connection timed out
How can I get these API results? Worth noting:
1) I have changed a few characters from my actual API key to make the one above
2) API documentation is here: https://publicapi.ohgo.com/docs/v1/swagger/index#/Reported32Conditions

It's failed to connect to API server. Try to increase connect timeout for that.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); // value in seconds

Try to skip ssl verification this will solve your issue
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://publicapi.ohgo.com/api/v1/reported-conditions?region=ne-ohio');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Skip SSL Verification
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: APIKEY 016c348e-58fe-486f-ad72-9b2f41c55a56';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Related

Google Drive v3 API : List all folders working on sandbox but not working on a curl call

Hi so I'm trying to get a list of all the folders I have on a specific drive. It is working when I do tests on google's api explorer here https://developers.google.com/drive/api/v3/reference/files/list but for some reason when I convert it into a php curl call It doesn't fetch any files
Here's my php curl code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_REFERER, 'referer here i removed it');
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files?corpora=allDrives&includeItemsFromAllDrives=true&includeTeamDriveItems=true&q=%27'.$folder_id.'%27%20in%20parents%20and%20mimeType%3D%27application%2Fvnd.google-apps.folder%27&supportsAllDrives=true&key'.$_api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authorization: Bearer '.$_access_token;
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo "<pre>";
var_dump(json_decode($result, true));
echo "</pre>";

PHP CURL - Apple push notification servers

I am writing code to send notifications to the Apple push notification servers (APNs) using PHP Laravel. It says in the documents that it requires HTTP/ HPACK header compression.
I've tried using cURL
$cURLConnection = curl_init();
if(strcmp(env('APP_ENV'), 'production') == 0) {
curl_setopt($cURLConnection, CURLOPT_URL, 'api.push.apple.com:443');
} else {
curl_setopt($cURLConnection, CURLOPT_URL, 'api.development.push.apple.com:443');
}
curl_setopt_array($cURLConnection, [
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_HTTP_VERSION =>CURL_HTTP_VERSION_2_0,
]);
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array(
'path: /3/device/<devicetoken>',
'authorization: bearer ' . $token,
'Content-Type: application/json'
));
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt ($cURLConnection, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
$apiResponse = curl_exec($cURLConnection);
but the APNS server always returning 'Empty reply from server'
I see a few things that could be problematic.
Try adding the following to actually turn your request into a POST request.
curl_setopt($c, CURLOPT_POST, 1);
Your 'path' should also be part of the main URL instead of being added as a header.
curl_setopt($c, CURLOPT_URL, "https://api.push.apple.com:443/3/device/<devicetoken>");
In addition to the previous suggestion, we should also be able to use certificate bases auth system.
Example snippet below:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://path-to-auth-example.com/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_cert");
curl_setopt($ch, CURLOPT_SSLCERT, "path/to/your/pem-file.pem");
curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");
$headers = [];
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
//catch your error wisely
}
curl_close($ch);
$result = json_decode($result);
print_r($result);
?>

Curl returns empty when response is too big

I am sending curl request like
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/Search');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"apikey\": \"xxxxxxxxxxxxxxx\",\"min\": 20, \"items_per_page\": 5000 }");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Requested-With: XMLHttpRequest';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
Now, when "items_per_page" parameter is set to under 4000 it works well everytime, but when its default 5000 it returns empty response.
I tryed to find the error, but its doesn't return anything
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_getinfo returns http_code 504.
I also tryed adding:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10000 );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
etc. but still can't make it work.

PHP + Dropbox API V2 - show thumbnail

I'm trying to display five images from my Dropbox account through PHP.
Listing them works perfectly using https://api.dropboxapi.com/2/files/list_folder and curl.
The json output, I turn into a PHP array, which tells me what files I need.
Subsequently, I call the images using https://content.dropboxapi.com/2/files/get_thumbnail:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/get_thumbnail");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CAINFO, "cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"path":"/images/1234.jpg","format":{".tag":"jpeg"},"size":{".tag":"w1024h768"}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Bearer SECRET_CODE";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
According to documentation this returns some metadata and the main body is the image. Now, what I'm struggling with is how to display the image, as if you were directly accessing the image through the URL (so I can out it in an <img src="">).
I recon I need header('Content-Type: image/jpeg'); but I have no other clue on how to continue.
Any help would be greatly appreciated.
Best,
Knal
Get thumbnail request are via the header rather than a Post request. try
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/get_thumbnail");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CAINFO, "cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$headers = array();
$headers[] = "Authorization: Bearer SECRET_CODE";
$headers[] = "Dropbox-API-Arg: {"path":"/images/1234.jpg","format":{".tag":"jpeg"},"size":{".tag":"w1024h768"}";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

convert Curl to php

Is it possible to convert this curl command to PHP?
curl -H "cwauth-token: CwEqP_oQXCiDKt3ph0leZ2AlKsqE6E4N " "https://reports.voluum.com/report?from=2014-09-24T00%3A00%3A00Z&to=2014-0925T00%3A00%3A00Z&tz=Europe%2FWarsaw&sort=visits&direction=desc&columns=campa ignName&columns=visits&columns=clicks&columns=conversions&columns=revenue&co lumns=cost&columns=profit&columns=cpv&columns=ctr&columns=cr&columns=cv&colu mns=roi&columns=epv&columns=epc&columns=ap&columns=errors&groupBy=campaign&o ffset=0&limit=100&include=active"
Here's the fish:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://reports.voluum.com/report?from=2014-09-24T00%3A00%3A00Z&to=2014-0925T00%3A00%3A00Z&tz=Europe%2FWarsaw&sort=visits&direction=desc&columns=campa ignName&columns=visits&columns=clicks&columns=conversions&columns=revenue&co lumns=cost&columns=profit&columns=cpv&columns=ctr&columns=cr&columns=cv&colu mns=roi&columns=epv&columns=epc&columns=ap&columns=errors&groupBy=campaign&o ffset=0&limit=100&include=active");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Cwauth-Token: CwEqP_oQXCiDKt3ph0leZ2AlKsqE6E4N";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Here's how to fish:
https://curl.haxx.se/docs/manpage.html
https://secure.php.net/manual/en/function.curl-setopt.php
And here's a few fishing machines you can use:
http://curl.trillworks.com/#php
https://incarnate.github.io/curl-to-php/
And BTW, here's why you should consider fishing alternatives:
Requests for PHP
Guzzle, PHP HTTP Client

Categories