使用cURL将文件发送到带有PHP的API
我正在尝试将文件上载到此API: 我对卷发一无所知。我找到了示例代码,并尝试了以下方法:使用cURL将文件发送到带有PHP的API,php,api,curl,Php,Api,Curl,我正在尝试将文件上载到此API: 我对卷发一无所知。我找到了示例代码,并尝试了以下方法: <?php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http://www.noelshack.com/api.php'); // send a file curl_setopt($curl, CURLOPT_POST, true); curl_setopt( $curl, CURLOPT_POSTFIELDS,
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.noelshack.com/api.php');
// send a file
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt(
$curl,
CURLOPT_POSTFIELDS,
array(
'fichier' => '@http://cdn.soccerwiki.org/images/player/2386.jpg'
));
// output the response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
print_r($result);
curl_close($curl);
?>
有人能解释一下我现在的代码不起作用吗?
它应该返回如下url:
但它没有返回任何东西。我的代码中没有任何错误,文件只是没有上传,我不知道为什么,因为我不知道如何显示api错误
提前谢谢
(对不起我的英语,我是法国人)有两个潜在问题
http://
指定文件@
<?php
ini_set('display_errors', 1);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.noelshack.com/api.php');
curl_setopt($curl, CURLOPT_VERBOSE, 1);
//curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0');
//curl_setopt($curl, CURLOPT_ENCODING, '');
//curl_setopt($curl, CURLOPT_REFERER, 'http://www.noelshack.com/api.php');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// download image to temp file for upload
$tmp = tempnam(sys_get_temp_dir(), 'php');
file_put_contents($tmp, file_get_contents('http://cdn.soccerwiki.org/images/player/2386.jpg'));
// send a file
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt(
$curl,
CURLOPT_POSTFIELDS,
array(
'fichier' => new CURLFile($tmp),
'submit' => 'Envoyer',
));
// output the response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
var_dump($result, curl_error($curl));
unlink($tmp); // remove temp file
curl_close($curl);
我添加了这段代码来查看curl是否返回错误:if(curl_exec($curl)==false){echo'curl error:'.curl_error($curl);}否则{echo“没问题”;}它返回“没问题”!您好@drew010我正在使用PHP7.1.1并尝试使用CURL上传图像。但不成功这里是我的代码