Upload a file using an API PUT request - php

I'm building an API in PHP. One of the methods is place.new (PUT request). It expects several string fields, and it also expects an image. However I can't get it working. With a POST request it was easy, but I'm not sure how to do it with a PUT and how to get the data on the server.
thanks for the help!
Test CURL code
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_INFILE, $image);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($image));
$this->result = curl_exec($curl);
curl_close($curl);
Server code
if ( $im_s = file_get_contents('php://input') )
{
$image = imagecreatefromstring($im_s);
if ( $image != '' )
{
$filename = sha1($title.rand(11111, 99999)).'.jpg';
$photo_url = $temp_dir . $filename;
imagejpeg($image, $photo_url);
// upload image
...
}
}
Solution
send
// Correct: /Users/john/Sites/....
// Incorrect: http://localhost/...
$image = fopen($file_on_dir_not_url, "rb");
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_INFILE, $image);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file_on_dir_not_url));
$result = curl_exec($curl);
curl_close($curl);
receive
/* Added to clarify, per comments */
$putdata = fopen("php://input", "r");
/* Open a file for writing */
$fp = fopen($photo_url, "w");
/* Read the data 1 KB at a time
and write to the file */
while ($data = fread($putdata, 1024))
{
fwrite($fp, $data);
}
/* Close the streams */
fclose($fp);
fclose($putdata);

Did you read http://php.net/manual/en/features.file-upload.put-method.php ? Script PUT /put.php all set up?
Also, what is $image -- it needs to be a file handler, not a file name.
Ps. Using file_get_contents will try to load whatever is PUT on the server into memory. Not a good idea. See the linked manual page.

Related

How to resize image before uploading to CDN using CURL

I have to two different domain, one is for CDN and the other for my application, am trying to upload image from example1 to cdn.example 2 using CURL, it is working perfectly well but my problem is to resize the image to get the dimension i want before saving it. I don't know if this will be done on the user site or the CDN site and i also don't know how i can resize it as it was read before posed. I have a function to resize image after it has been uploaded but i never trying while uploading can someone help me please
example.com/upload.php
<?php
$image_file = (isset($_FILES['Image']) ? $_FILES['Image'] : null);
if(!empty($image_file)){
$filename = $image_file['tmp_name'];
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$post_var = array(
'image' => base64_encode($data),
'id' => 100
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://cdn.example.com/saveimage.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . upload_cdn_server_key));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_var);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$out = curl_exec($curl);
$err = curl_error($curl);
curl_close ($curl);
$pms = json_decode($out,true);
$url= $pms['data']['link'];
if(!empty($url)){
$image_uploaded = true;
}
}
cdn.example.com/saveimage.php
if(isset($_POST["image"], $_POST["id"])){
$encoded_file = $_POST['image'];
$productid = $_POST['id'];
$filepath = __DIR__ . '/image/gallery/p/';
$filename = md5($productid.date('Y-m-d H:m:s i')).'-1.jpeg';
$uploadeImage = file_put_contents($filepath.$filename, $decoded_file);
}

cURL request works on localhost but Connect() times out on server

OK, so I apologise if this has been dealt with before, after searching for 30 minutes I couldn't find anything that exactly matched my issue.
I have a bit of a strange situation where a client is using an FTPS connection which I need to use cURL to download a file from. While running this EXACT script on my localhost the files download with no issue however once I place this on the server (our own hosting) the cURL returns a timeout error.
$remote = [
filename => URL
];
ini_set("display_errors", 1);
foreach ($remote as $key => $file) {
$targetFile = file formatted;
$sourceFile = $file;
$ftpuser = username;
$ftppassword = password;
echo $targetFile;
// function settings
$timeout = 10;
$fileOpen = 'w';
$curl = curl_init();
$file = fopen($targetFile, $fileOpen);
curl_setopt($curl, CURLOPT_URL, $sourceFile);
curl_setopt($curl, CURLOPT_USERPWD, $ftpuser . ':' . $ftppassword);
// curl settings
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($curl, CURLOPT_FTP_SSL, CURLFTPSSL_ALL);
//curl_setopt($curl, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_FILE, $file);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
var_dump ($info);
echo curl_error($curl);
curl_close($curl);
fclose($file);
Most likely the server is being blocked by a proxy server, preventing the connection from being established.

Parse web page without save local?

Sorry my English a little. I am using CURL because web page is required this function. I don't get file_get_contents of page. How to parse page without page save? (fopen,fwrite)
<?PHP
function fileGet($url, $timeout = 55, $ref=true){
$useri = $_SERVER['HTTP_USER_AGENT'];
#set_time_limit($timeout);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=fztitsfgsafafaq25llwafd0; path:/' );
curl_setopt($curl, CURLOPT_USERAGENT, $useri);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_REFERER,$url);
$data = curl_exec($curl);
curl_close($curl);
// Save Page Start
$fp = fopen('data.html', 'w');
fwrite($fp, $data);
fclose($fp);
// Save Page End
return $data;
}
// Start Code
fileGet("http://www.example.com",10); // Start Function
$html = file_get_html('data.html'); // Open Saved Page In Local
foreach($html->find('div.columns') as $article) {
// Events.....
mysql_query("Insert Query");
}
// End Code
?>
<?PHP
function fileGet($url, $timeout = 55, $ref=true){
$useri = $_SERVER['HTTP_USER_AGENT'];
#set_time_limit($timeout);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=fztitsfgsafafaq25llwafd0; path:/' );
curl_setopt($curl, CURLOPT_USERAGENT, $useri);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_REFERER,$url);
$data = curl_exec($curl);
return $data;
}
// Start Code
$html = str_get_html(fileGet("http://www.example.com",10));
foreach($html->find('div.columns') as $article) {
// Events.....
mysql_query("Insert Query");
}
// End Code
?>

I am using curl to download profile photo. Is that right method?

function getFacebookPhoto($img,$fullpath) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $img);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
$user_curl_image = curl_exec($curl);
curl_close($curl);
if(file_exists($fullpath)){
#unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $user_curl_image);
fclose($fp);
}
This is the script i use to download photos.
Is it rightway to do it?
I use this function inside facebook apps.

How to send a file with curl to the controller

I have an url with an image: http://www.example.com/image.jpg
and I have an action in the controller that uploads a file to the server by user input. I want to use this same action to upload a file by url. I started to get the data from image:
$url = "http://www.example.com/image.jpg";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
Now how do I send the $rawdata to the controller to be handled like a normal user upload?
Thanks
I figured it out myself.
$url = "http://www.example.com/image.jpg";
$filename = basename($url, rand_string(7));
$path = realpath("../webroot/uploads/")."/".$filename;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($path)){
unlink($path);
}
$file = fopen($path, 'x');
fwrite($file, $rawdata);
fclose($file);
$post_data['Filedata'] = '#'.$path;
$url = "http://localhost/controller/action";
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
echo $response;
In the controller:
function action(){
//get properties of the file
//$_FILES['Filedata']['name'];
//$_FILES['Filedata']['size'];
//$_FILES['Filedata']['tmp_name'];
//savefile();
}
If anyone knows how to post the file without saving it will be great.

Categories