convert Curl to php - 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

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 - 403 Error With API Key In Header

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);

Using cURL GET statement in PHP

I have a cURL GET statement:
curl -v -X GET "https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/{stationId}" -H "Ocp-Apim-Subscription-Key: {subscription key}" --data-ascii "{body}"
that works fine in a terminal window (with my sub key) - I now want to use it in a PHP script to retrieve the associated JSON. Code suggestions for achieving this appreciated...
Not tested but you need something like this with PHP CURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/{stationId}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_TRANSFERTEXT, true);
$headers = array();
$headers[] = 'Ocp-Apim-Subscription-Key: {subscription key}';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

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);

Create object with data from cURL

So i'm using cURL with PHP to retrieve some data from a JSON file. The cURL script returns something like this: https://s4.postimg.org/43svl8rot/image.png
I'm using this PHP script:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://test.pt/api/objgroupinfo/16Jcr05g37KpLklz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://test.pt/api/dataout/IAfhAfTIUZrCje5q.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxx";
$headers[] = "X-Startdate: 2016-10-01 00:00:00";
$headers[] = "X-Enddate: 2016-10-10 15:00:00";
$headers[] = "X-Channelnum: 0";
$headers[] = "X-Reclimit: 200";
$headers[] = "User-Agent: test/1.0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$json = json_decode($result,true);
print_R($json);
curl_close ($ch);
Can someone help me on how to turn this data into an object or array that i can use? I need to acess this data to show it graphically
json_decode will convert a JSON encoded string to a PHP variable, see json_decode. It looks like your code is already doing this.

Categories