使用API PUT请求上传文件 [英] Upload a file using an API PUT request
问题描述
我在PHP中构建一个API。其中一种方法是place.new(PUT请求)。它期望几个字符串字段,它也期望一个图像。但我不能得到它的工作。使用POST请求很容易,但我不知道如何做一个PUT和如何获取服务器上的数据。
感谢您的帮助!
测试CURL代码
$ 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);
服务器代码
if($ im_s = file_get_contents('php:// input'))
{
$ image = imagecreatefromstring($ im_s);
if($ image!='')
{
$ filename = sha1($ title.rand(11111,99999))。
$ photo_url = $ temp_dir。 $ filename;
imagejpeg($ image,$ photo_url);
//上传图片
...
}
}
$ b b
解决方案
发送
/正确:/ Users / john / Sites / ....
//不正确: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);
接收
/ *添加以澄清,每个注释* /
$ putdata = fopen(php:// input,r);
/ *打开要写入的文件* /
$ fp = fopen($ photo_url,w);
/ *一次读取数据1 KB
并写入文件* /
while($ data = fread($ putdata,1024))
{
fwrite($ fp,$ data);
}
/ *关闭流* /
fclose($ fp);
fclose($ putdata);你读过
/php.net/manual/en/features.file-upload.put-method.php\">http://php.net/manual/en/features.file-upload.put-method.php ? Script PUT /put.php
所有设置?
此外, $ image
- 它需要是一个文件处理程序,而不是文件名。
Ps。使用 file_get_contents
将尝试将服务器上的任何PUT加载到内存中。不是一个好主意。请参阅链接的手册页。
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.
这篇关于使用API PUT请求上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!