I am using this code... but i am getting some curl error in my code..
function send_data($data) {
$url = ""; // URL to POST FORM. (Action of Form)
$url .= '?acx=' . urlencode('V2ViSnNvbkRldkFwbkBTaWdtYUFwbjpXZWIxMjMkSnNvbg==') . '&key=' . urlencode('QW51bWF0aGlVbmRhQFNpZ21hQXBuOlVuZHVBbnVtYXRoaQ==');
$url .= '&impdata=' . urlencode('{"System":{"Client":"SigmaApn","ACX":"V2ViSnNvbkRldkFwbkBTaWdtYUFwbjpXZWIxMjMkSnNvbg==","KEY":"QW51bWF0aGlVbmRhQFNpZ21hQXBuOlVuZHVBbnVtYXRoaQ==","ClientIP":"74.117.104.99","Mode":"TEST"},"Module":{"Process":"BatchEnrollment","Version":"2.1.5","DataDefinition":[{"Name":"enrolment_queue","IncludeRecType":"false","Identifier":"sys_batch_no","Header":["batch_no","source","source_line","agent_code","premise_id","plan_group","request_date","enrol_type","cust_firstname","cust_lastname","phone1","cm_address1","cm_city","cm_state","cm_zip","life_support","cust_status","plan_id1","contract_ind","contract_type","calc_method","rate_type","adder1_rate","cust_bill_type","edi_bill_presenter"]}],"Data":[["Batch","BATCH","1","0","PREMISE1","R","10/28/2011","M","' . $data['personalinfo_first_name'] . '","' . $data['personalinfo_last_name'] . '","8050123481","' . $data['service_address1'] . '","' . $data['service_city'] . '","' . $data['service_state'] . '","' . $data['service_zipcode'] . '","N","P","PN001","Y","MCP2","1","4","0.02","Default","ESP"]]}}');
$url .= '&mode=' . urlencode('TEST');
$headersVal = array("application/x-www-form-uriencoded");
$myFile = "/tmp/isigma.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_STDERR, $fh);
curl_setopt($ch, CURLOPT_POST, 1); // use this option to Post a form
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, '1');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/iSIGMARootCA.crt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . "/APN_Generic_JSON.pem");
curl_setopt($ch, CURLOPT_SSLKEY, getcwd() . "/apnakey.pem");
curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free system resources.
fclose($fh);
$res_array = json_decode($result, true);
if (!empty($result)) {
return $res_array;
} else {
echo "Curl Error" . curl_error($ch);
}
}
thanks for help in advance..
I'm using this function to post data.
You can put an associative array as second parameter, the first is the url that you want to call.
private function postData($url, $data) {
$post_data = http_build_query($data);
$fp = tmpfile();
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, '/dev/null');
$status = curl_exec($curl);
$response['http_code'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status) {
$response['error'] = curl_error($curl);
$response['errno'] = curl_errno($curl);
}
curl_close($curl);
rewind($fp);
$response['data'] = '';
while ($str = fgets($fp, 4096)) {
$response['data'] .= $str;
}
fclose($fp);
return $response['data'];
}
Related
The task - there is a direct link to download the file, you need to use php methods to download this file and save it on the server.
There is code, but it does not load the file, but only creates a new one.
<?php
$token = 'token';
// file from yandex disk.
$yd_file = '/FermerPrice/test.jpeg/';
// save this.
$path = download;
//get array with link
$ch = curl_init('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);
// $res['href'] - link download
$res = json_decode($res, true);
if (empty($res['error'])) {
$file_name = $path . '/' . basename($yd_file);
$file = #fopen($file_name, 'w');
$ch = curl_init($res['href']);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($file);
echo "Good.";
}
?>
You can use this:
file_put_contents("localfilename", file_get_contents('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file)));
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;
}
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
I am having trouble doing cURL PUT in PHP for a REST API. It says 401 Unauthorized and I am quite new to php so I didn't know what to do please help me.
<?php
$url = "https://app.linkemperor.com/api/v2/vendors/blasts/6949235.json";
$data = '{"blasts": {"links" : "http://doblajemexicano.com.mx/sitio/index.php?title=Keyword_Elite_,_The_Ferrari_Of_Search_phrases_Study
http://fi.caravanwiki.com/index.php/Keyword_Elite_,_The_Ferrari_Of_Keywords_and_phrases_Research"}}';
$content = json_encode($data);
$oneMB = 1024 * 1024;
$fh = fopen('php://temp/maxmemory:$oneMB', 'r+');
fwrite($fh, $content);
rewind($fh);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, "<api key>:x");
curl_setopt($curl, CURLOPT_INFILE, $fh);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($content));
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
?>