PHP + Dropbox API V2 - show thumbnail - php

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

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

Uploading Textfiles to GCP-Storage with php changes the content of the file

I would like to upload a textfile to Google Cloud Storage.
I use this function in php:
function upload_object_to_Google_bucket($bucket,$accesstoken,$proxy,$objectname,$objectpath)
{
#Build the CUrl: incarnate.github.io/curl-to-php
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "https://storage.googleapis.com/upload/storage/v1/b/".$bucket."/o?uploadType=media&name=".$objectname);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
# curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$file=$objectpath.$objectname;
$item=make_curl_file($file);
print_r($item);
$file = $objectpath.$objectname;
$post = array(
$item
);
# curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
# curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# curl_setopt($ch, CURLOPT_NOBODY, true);
$headers = array();
$headers[] = 'Authorization: Bearer '.$accesstoken;
$headers[] = 'Content-Type: '.object_to_array($item)["mime"];
$headers[] = 'Content-Length: '.filesize($file);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
print_r($response);
if (curl_errno($ch)){
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
}
It's all fine, but the textfile has become new content in the Bucket.
--------------------------9ad622dfd933f230
Content-Disposition: form-data; name="0"; filename="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xml"
Content-Type: text/plain
These lines are added to the file. Does anyone has an idea whats the problem and how I can avoid this behaviour?

CURL with PHP to Azure

How can create one workitem with CURL with PHP:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://dev.azure.com/fernandodomenike/fernando/_apis/wit/workitems/$Task?api-version=5.1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
'file' => '#' .realpath('azure.json')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ':' . 'secret');
$headers = array();
$headers[] = 'Content-Type: application/json-patch+json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I try execute with command line Windows and work.
Whats is my wrong?
File upload via # is a feature from command line cURL client, you have to implement your own code on PHP to do that.
Example:
$json = file_get_contents('azure.json');
// Attach JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
I hope it helps you.

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

Flipkart Api: Getting error while trying to retrieve data from api

This is the curl, php program i gave to retrieve data from flip kart using there API. But what ever i do , i am getting the same error as :
{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"}
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.flipkart.net/oauth-service/oauth/token?grant_type=client_credentials&scope=Seller_Api,Default");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'appid:appsecret';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
print $server_output ;
curl_close ($ch);
?>
I dont know what is the error , i checked number of time if the problem is with appid and appsecret i gave but still its showing same error.
Flipkart Documentation Link
$action = 'https://sandbox-api.flipkart.net/oauth-service/oauth/token?grant_type=client_credentials&scope=Seller_Api';
$headers = array();
$headers[] = 'content-type: application/json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $action);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "<id>:<key>");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$result = curl_exec($curl);
echo '<pre>';print_r($result);
curl_close($curl);

Categories