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);
}
}
Related
my PHP Request to tag some images doesnt work, i get an error.
{"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}
Here is my PHP Script with Curl.
$ocpApimSubscriptionKey = 'MYKEY 1 FROM ccounts/cskeys';
$uriBase = 'https://MY ENDPIINT FROM overview.cognitiveservices.azure.com/';
//$uriBase = 'https://westeurope.api.cognitive.microsoft.com/';
$request_URL = $uriBase . 'vision/v3.0/tag';
//$request_URL = $uriBase . 'vision/v3.1/analyze?visualFeatures=Categories,Description,Tags';
$params = array('language' => 'en');
$request_URL = $request_URL . '?' . http_build_query($params);
$headers = array(
'Content-Type' => 'application/json',
'Ocp-Apim-Subscription-Key' => $ocpApimSubscriptionKey,
'Ocp-Apim-Trace' => true
);
$imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg';
$body = json_encode(array(
'url' => $imageUrl
));
print_r($body);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request_URL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
Any Ideas?
Your $headers is wrong, below code is works for me.
<?php
// Your code here!
$ocpApimSubscriptionKey = 'b641******47c5558f2b';
$uriBase = 'https://eastus.api.cognitive.microsoft.com/';
$request_URL = $uriBase . 'vision/v3.0/tag';
$params = array('language' => 'en');
$request_URL = $request_URL . '?' . http_build_query($params);
// error code, I don't use
$headers = array(
'Content-Type' => 'application/json',
'Ocp-Apim-Subscription-Key' => $ocpApimSubscriptionKey,
'Ocp-Apim-Trace' => true
);
$imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg';
$data = array("url" => $imageUrl);
$body = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request_URL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
// here is correct usage
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Ocp-Apim-Subscription-Key: b6417ac85*****7c5558f2b'
));
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
?>
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 use a funtion to download files trough a private API.
Everything goes fine to download small/medium files, but large files is impossible beacause it uses too much memory.
Here is my function:
protected function executeFile($method, $url, $params=array(), $as_user=null) {
$data_string = json_encode($params);
$method = strtoupper($method);
$ch = curl_init();
if($method == 'GET') {
$url = $this->options['api_url'].$url.'?';
$url .= $this->format_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
curl_setopt($ch, CURLOPT_URL, $this->options['api_url'].$url);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($as_user) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Token: '.$this->token,
'As: '.$as_user
));
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Token: '.$this->token
));
}
$result_json = curl_exec($ch);
$curl_info = curl_getinfo($ch);
$return = array();
$return["result"] = $result_json;
$return["entete"] = $curl_info;
return $return;
}
How could i optimize this to download files to disk instead of memory ?
Thanks
use CURLOPT_FILE . It will ask for file pointer where download will be saved.
code will be like
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fp = fopen("your_file", 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
You can use CURLOPT_FILE, like this:
protected function executeFile($method, $url, $params=array(), $as_user=null) {
$data_string = json_encode($params);
$method = strtoupper($method);
$fp = fopen ('savefilepath', 'w+');
$ch = curl_init();
if($method == 'GET') {
$url = $this->options['api_url'].$url.'?';
$url .= $this->format_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
curl_setopt($ch, CURLOPT_URL, $this->options['api_url'].$url);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
....
curl_exec($ch);
fclose($fp);
.....
return $return;
}
<?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
The email does not pause to API
<?php
function post_to_url($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
echo $result;
curl_close ($ch);
}
$data = array( "email1" => "myemail#yahoo.com" );
post_to_url("http://api.payquicker.com/api/IsActiveAccount?email=", $data);
?>
echo $result; should be----> http://api.payquicker.com/api/IsActiveAccount?email=myemail#yahoo.com
but im only getting http://api.payquicker.com/api/IsActiveAccount?email=
echo "<p><strong>SEND INVITATION</strong></p></BR>";
$dataw = array(
"authorizedKey" => "xxx-xxxx-xxx",
"senderEmail" => "senderemail#yahoo.com",
"recipientEmail" => "myemail#yahoo.com"
);
$url_send ="https://api.payquicker.com/api/SendInvitation?authorizedKey=xxx-xxxx-xxx";
$str_data = json_encode($dataw);
function send_invitation($url, $post_data){
$url_send =$url;
$ch = curl_init($url_send);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Content-Length: ' . strlen($post_data))
);
$result = curl_exec($ch);
//$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}
echo "RESULT :" . send_invitation($url_send, $str_data);