<?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
Related
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);
}
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);
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;
I developed a script with PHP which I use with the console, with the command php index.php.
In this script, I use cURL to query a server.
The problem is my script shows logs on the console, and I just want the result of the echo.
Do you have an idea to hide these logs?
// GET TOKEN
// /////////////////////////////////////////////////////////////////////////////
$url = "some/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pwd");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
$location = "";
preg_match_all('/^Location:(.*)$/mi', $data, $location);
$location = trim($location[1][0]);
$location = parse_url($location);
parse_str($location['query'], $attrs);
$token = $attrs['code'];
if( isset($token) ) {
// GET CONNEXION
// /////////////////////////////////////////////////////////////////////////
$url = "some/url";
$post_data = ['code' => $token, 'grant_type' => 'authorization_code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$client_id:$client_secret");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = json_decode(curl_exec($ch));
$connexion = isset($data->access_token) ? $data->access_token : $data->error_description;
// GET LOGIN
// /////////////////////////////////////////////////////////////////////////
$url = "some/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$data = json_decode($data);
// $login = json_encode(['public' => $data->api_keys[1]->public, 'secret' => $data->api_keys->secret]);
// REQUEST
// /////////////////////////////////////////////////////////////////////////
$url = $request . "?externalId=".$external_id."&externalSource=".$external_source;
date_default_timezone_set("Europe/Paris");
$nonce = generateRandomString();
file_put_contents('php://stderr', print_r("Set random nonce to " . $nonce . "\n", TRUE));
$created = date("Y-m-dTH:i:sP");
$created = date("Y-m-dTH:i:sP");
$username = $data->api->public;
$secret = $data->api->secret;
$pwd_digest = base64_encode(sha1($nonce.$created.$secret));
$auth_header = "X-WSSE: UsernameToken Username=\"$username\", PasswordDigest=\"$pwd_digest\", Nonce=\"$nonce\", Created=\"$created\"";
$header = array($auth_header, 'Accept: something', 'Accept-Language: en');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
$data = json_decode($data);
echo $data->nbNqPoints;
} else {
echo "0";
}
Answer found at this address: managing curl output in php
I add this line for every cURL instance :
curl_setopt($ch, CURLOPT_VERBOSE, 0);
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);
}
}