Send file CURL + PHP 7 - php

I use code for send zip file and this work good:
$file = ''.$_SERVER['DOCUMENT_ROOT'].'/projects/file.zip';
$data = array(
"file" => new CURLFile($file),
"data" => '{"title":"Test"}'
);
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, 'https://example.com');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;',
));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$final = curl_exec($handle);
$response = json_decode($final, true);
curl_close ($handle);
But when I send a file with the .keystore extension, nothing works. I do not receive errors on the server.
How do I properly send such a file?

you can try this
$file = ''.$_SERVER['DOCUMENT_ROOT'].'/projects/file.zip';
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new CURLFile($file, $mime, $name);
$data = array(
"file" => $output,
"data" => '{"title":"Test"}'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = curl_error($ch);
}
curl_close ($ch);

Related

How to post image using curl in php

I want to change profile image another application curl request i am trying below code can anyone help me please
$viewer = Engine_Api::_()->user()->getViewer();
$apiData = array(
"email" => $viewer->email,
"profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";
$response = $this->callRiseAPI2($apiData,$apiHost);
private function callRiseAPI2($apiData,$apiHost){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
//return the API response
return json_decode($jsonData);
}
As Anoxy said, you need to put in the header the Content-Type :
$viewer = Engine_Api::_()->user()->getViewer();
$apiData = array(
"email" => $viewer->email,
"profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";
$response = $this->callRiseAPI2($apiData,$apiHost);
private function callRiseAPI2($apiData,$apiHost){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: multipart/form-data');
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
//return the API response
return json_decode($jsonData);
}

Zoho API-V2 Add Attactmetn URL

I am trying to add attachment url in crm. I am flowing this documentation . But i got error !
This is my code :
$zoho_url = "https://www.zohoapis.com/crm/v2/$module/$id/Attachments";
$post['attachmentUrl'] = $url;
$ch=curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_URL,$zoho_url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$headers = array();
$headers[] = "Authorization: ".$authtoken;
$headers[] = "Content-Type: multipart/form-data";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_errno($ch);
curl_close ($ch);
if ($err) {
$result = $err;
} else {
$result = $response;
}
print_r($result);
This is response :
{"code":"INVALID_REQUEST","details":{},"message":"unable to process your request. please verify whether you have entered proper method name, parameter and parameter values.","status":"error"}
I made this code for attach a file in a zoho record.
//Get the oauth Token
$accessToken=getCurrentAccessToken();
// files to upload
$tmpfile;
$filename;
$type;
foreach($files as $file){
$tmpfile = $file['tmp_name'];
$filename = basename($file['name']);
$type = $file['type'];
}
$cfile = new CURLFile(realpath($tmpfile),$type,$filename);
$post_data = array (
'file' => $cfile
);
$url = "https://www.zohoapis.com/crm/v2/$module/$id/Attachments";
$headers = array(
'Content-Type: multipart/form-data',
sprintf('Authorization: Zoho-oauthtoken %s', $accessToken)
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
return $response;

Dialog IBM - Curl - PHP Laravel

Watson has the next code:
curl -u "{username}":"{password}"
-X POST
--form file=#template.xml
--form name=templateName
"https://gateway.watsonplatform.net/dialog/api/v1/dialogs"
And I have:
$ch = curl_init();
$data['name'] = $this->post['name'].";type:form";
$data['file'] = $this->getCurlValue($this->post['file'], $this->post['nombre']);
curl_setopt($ch, CURLOPT_URL, $this->url.$this->metodo);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
if(count($this->post) > 0){
//curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($ch);
curl_close($ch);
json_decode($output);
$this->post has...
$data = Request::only('name', 'file');
And I only get as answer: {}
What I'm missing?
Thanks everyone!... First time try to send a uploading file with curl :c
It was simpler than I imagined.
Store:
$data = Request::only('name', 'file');
$nombre = time().".".Request::file('file')->getClientOriginalExtension();
Request::file('file')->move(base_path() . '/public/dialogos/', $nombre);
$data['path'] = base_path() . '/public/dialogos/'.$nombre;
$data['file'] = $_FILES['file'];
Curl:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.$this->metodo);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
if(count($this->post) > 0){
$datos['name'] = $this->post['name'];
$datos['file'] = $this->getCurlValue($this->post['file'], $this->post['path']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datos);
}
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output);
getCurlValue($file, $path);
if (function_exists('curl_file_create')) {
return curl_file_create($path, $file['type'], $file['name']);
}
$value = "#{$path};filename=" . $file['name'];
$value .= ';type=' . $file['type'];
return $value;
Stupid msg that said I need PUT or DELETE methods... haha

'computer says yes' error API ge.tt upload

<?php
$UploadURL = $CreatFile->upload->posturl;
$file = $_SERVER['DOCUMENT_ROOT'] . '/picasa/preview_alpha.jpg';
$post = array(
'filename' => '#'.$file
);
$header = array(
"Content-Length: ".filesize($file),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $UploadURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$hasil = curl_exec($ch);
$hasil1 = curl_error($ch);
curl_close($ch);
return $hasil.$hasil1;
Result:
computer says yes
1
but in the share file, the file didn't upload
if use puturl then it can't conect to server
https://open.ge.tt/1/doc/rest#files/{sharename}/create

How do you Upload Image to parse.com usint REST API + PHP?

Currently using this, but file that is stored is empty, so I supose no data is going through.
$post = array("file"=>'#'.$_FILES['uploadfile']['tmp_name']);
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id : XXXXXXXXXXXXXX','X-Parse-REST-API-Key: XXXXXXXXx', 'Content-type: image/jpeg'));
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/'.$_FILES['uploadfile']['name']);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
Got it working. You need to send the binary data as the post field. duh.
Before this you should probably create some restrictions (file size, type etc)
$post = file_get_contents($_FILES['uploadfile']['tmp_name']);
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id : XXXXXXXX','X-Parse-REST-API-Key: XXXXXXXXXXXXXXX', 'Content-type: image/jpeg'));
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/'.$_FILES['uploadfile']['name']);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
Use this work perfectly same way as we need.....
function sendImageToParse($url, $filename) {
$this->layout = FALSE;
$this->autoRender = false;
$headers = array(
'X-Parse-Application-Id:' . $this->APPLICATION_ID,
'X-Parse-REST-API-Key:' . $this->REST_API_KEY,
'Content-Type: image/jpeg'
);
$data = file_get_contents($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/' . $filename);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function add() {
if ($this->request->is('post')) {
$giftUniqueId = time();
$urlFileImage = $allData['Coupon']['imageLink'];
//Eg : "http://localhost/favrapp/img/couponsLogo/gap.jpg";
$filename = $allData['Coupon']['imageName']; //Eg : "gap.jpg";
$responseFile = $this->sendImageToParse($urlFileImage, $filename);
$arrayInt = json_decode($responseFile);
if (!empty($arrayInt)) {
$name = $arrayInt->name;
$url = $arrayInt->url;
} else {
$name = '';
$url = '';
}
//Save to table by creating object
$url = 'https://api.parse.com/1/classes/giftcard';
$data = array('name' => $allData['Coupon']['name'],
'couponType' => $allData['Coupon']['coupontype'],
'giftcardTypeid' => $allData['Coupon']['giftcardTypeid'], 'giftcardcheckid' => $giftUniqueId,
'logo' => array('name' => $name, '__type' => 'File', 'url' => $url),
);
$_data = json_encode($data);
$headers = array(
'X-Parse-Application-Id: ' . $this->APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $this->REST_API_KEY,
'Content-Type: application/json',
'Content-Length: ' . strlen($_data),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl);
curl_close($curl);
}
}

Categories