我正在尝试将文件上传到此 API:http://www.noelshack.com/api.php 我对 cURL 一无所知。我找到了示例代码,并且尝试了以下方法:
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
任何人都可以用我当前的代码来解释我不起作用吗?它应该返回这样的网址:http://www.noelshack.com/2016-XX-XXXXXXXXXX-2386.png
但它没有返回任何东西。我的代码中没有任何错误,文件只是没有上传,我不知道为什么,因为我不知道如何显示 api 错误!
提前致谢
(抱歉我的英语不好,我是法国人)
有 2 个潜在问题。
http://.CURLFile而不是在文件名前面添加@.这是解决这两个问题的一些代码:
<?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);
Run Code Online (Sandbox Code Playgroud)
如果您没有 PHP 5.5.0 或更高版本,只需删除该CURLFile结构并将其替换为'@' . $tmp,
| 归档时间: |
|
| 查看次数: |
14299 次 |
| 最近记录: |