imgur commerce - how upload image to album? - php

For upload image in album on imgur.com we are use it information and it code:
$image = file_get_contents($_FILES['upload']['tmp_name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://imgur-apiv3.p.mashape.com/3/image');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Client-ID ' . $client_id ));
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'X-Mashape-Key: ' . $xmash ));
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'image' => base64_encode($image) ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'album' => $album_id ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'type' => 'base64' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'name' => 'test_name' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'title' => 'test title' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'description' => 'blablabla' ));
$result= curl_exec($ch);
var_dump($result);
curl_close($ch);
but in result we are get error Invalid image type.
Tell me please why we are get it error and how upload image in album?
P.S.: we are test commerce accout.

Ensure your image is structured correctly first before sending the base64..
$baseContent = "";
if (isset($_FILES['upload']['tmp_name'])) {
$imgbinary = fread(fopen($_FILES['upload']['tmp_name'], "r"), filesize($_FILES['upload']['tmp_name']));
$baseContent = 'data:image/png;base64,' . base64_encode($imgbinary);
}
This will set the $baseContent var to the base64.. Then just pass through to your request :
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'image' => $baseContent ));
Looks like it's not formatting correctly. I havent tested this fully but it should ensure you have the correct base64.

Related

Json received = false

I am using ci framework, and i try to create notification using
onesignal API. I have hosted the website using https. But why my JSON
received is always false using onesignal API, but if i test using
postman it runs fine without error.
This is my code
function send_message($id, $nama) {
$content = array(
"en" => $nama
);
$heading =array(
"en" => $id
);
$fields = array(
'app_id' => "2cd5ad24-a2d9-4e7d-ac66-7494cebd8e84",
'included_segments' => array(
'Active Users'
),
'contents' => $content,
'headings' => $heading,
'url' => "https://esop.matahariled.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic MDQ0NjY0ZmYtZjQ2Yi00ODVmLTkzZjgtZmVkZDBkODk0MDFl'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_CAINFO, $_SERVER['DOCUMENT_ROOT']."/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}

Upload file via REST API, IPBoard

Im trying to upload files via IPBoards REST Api but dont really know how to format the files.
This is how it looks right now but only gets error:
{
"errorCode": "1L296\/B",
"errorMessage": "NO_FILES"
}
Code:
$post = array(
'category' => 1,
'author' => 1,
'title' => 'Test title',
'description' => 'test description',
'files' => "{'test.txt':'".file_get_contents('/home/test/test.txt')."'}",
);
$target_url = 'https://example.com/api/downloads/files';
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD,$apikey.":");
$result = curl_exec ($ch);
curl_close ($ch);
Here is the api documentation: API doc
Try using the http_build_query method and change the $post array slightly:
'files' => array($filename => $contents),
And change to:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

Can't upload file to BOX api

I wrote this code. I send data to $url = "https://upload.box.com/api/2.0/files/content", but i don't get a response. Maybe someone has also had this problem?
$absolutePath = 'home/my/path/uploads/media/ClientPDF/0001/01/c607bea86bdb42220bb49aac722b4a5eb44be3db.pdf';
$json = json_encode([
'name' => 'c607bea86bdb42220bb49aac722b4a5eb44be3db.pdf,
'parent' => ['id' => 7479666489] //parent folder
]);
$fields = [
'attributes' => $json,
'file' => #$absolutePath
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer MY_TOKEN_KEY'
'Content-Type:multipart/form-data'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($ch);
var_dump(json_decode($response, true)); die;
var_dump - print nothing, i can't debug this request. Please, help me to solve this problem

imgur with php - How upload image in my album (error 401 "Authentication required")?

For uploading image in albums on imgur.com it code:
if(isset($_FILES['upload']['tmp_name'])) {
$imgbinary = fread(fopen($_FILES['upload']['tmp_name'], "r"), filesize($_FILES['upload']['tmp_name']));
$image = 'data:image/png;base64,' . base64_encode($imgbinary);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://imgur-apiv3.p.mashape.com/3/image');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Client-ID ' . $client_id ));
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'X-Mashape-Key: '. $xmash)); //. $xmash
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'image' => $image ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'album' => $album_id ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'type' => 'base64' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'name' => 'test_name' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'title' => 'test title' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'description' => 'blablabla' ));
$reply = curl_exec($ch);
var_dump($reply);
curl_close($ch);
But now we are get error in answer:
string(112) "{"data":{"error":"Authentication
required","request":"/3/image","method":"POST"},"success":false,"status":401}"
In result we have some questions:
How i can auth (on php) ?
in docs https://market.mashape.com/imgur/imgur-9 need paste Authorization HEADER AUTH. How get him?
need to join these into one array and thus one set call:
$headers = array('Authorization: Client-ID ' . $client_id, 'X-Mashape-Key: ' . $Mashape_Key)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

how can i add items in youtubeplaylist?

I am trying to use this code to insert item in youtubeplaylist and its not working as i want ... i think its showing me if playlist contain this id .
please check and let me know what i need to change in code to insert this ID in my playlist .
Here is code :
$option = array(
'part' => 'snippet',
'mine' => 'true',
'key' => 'AIzaSyCJlbT_sqTG2nOUMYU24WzzxXpOelaiYTA',
'access_token' => $_SESSION['info']['access_token'],
'playlistId' => $ID,
'kind' => 'youtube#video',
'videoId' => 'KMGuyGY5gvY',
);
$url = "https://www.googleapis.com/youtube/v3/playlistItems?".http_build_query($option, 'a', '&');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curlheader[0] = "Authorization: Bearer " . $_SESSION['info']['access_token'];
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);
$json_response = curl_exec($curl);
curl_close($curl);
$responseObj = json_decode($json_response);
thanks in advance.

Categories