我试图使用curl put请求上传图像,并且如果图像存储在本地目录中,则能够上传图像。但问题是我必须从另一个图像网址上传图像。 任何人都可以帮助我。
$localfile = "testing.jpg";
// echo filesize($localfile); die;
$url = API_URL . "pages/343/files/=".$localfile."?description=testing";
$fp = fopen ($localfile, "r");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, API_USERNAME . ":" . API_PASSWORD);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
// $jsonOutput = json_decode($http_result, true);
curl_close($ch);
fclose($fp);
我试图将$ localfile更改为
$localfile = "https://someurl/someimage.png";
但它给了我以下错误。
Warning: curl_setopt(): cannot represent a stream of type tcp_socket/ssl as a STDIO FILE* in C:\xampp\htdocs\newPutMedia.php on line 29
警告:第30行的C:\ xampp \ htdocs \ newPutMedia.php中https://help.optimizely.com/hc/en-us/article_attachments/200205465/1._Site-Wide_Addition_gmc_BEFORE.png的文件大小():stat失败
答案 0 :(得分:0)
显然这是bug in PHP cURL。您可以将错误修复程序用作mentioned here,或者您可以执行此操作:
将文件从URL复制到本地目录:
file_put_contents(__DIR_\_ . "/testing.jpg", file_get_contents($url));
使用存储在服务器中的新文件来获取cURL PUT请求:
$localfile = __DIR_\_ . "/testing.jpg";
由于从服务器读取文件不存在错误,因此该错误不会导致错误,这将完美地发挥作用。