Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用cURL将文件发送到带有PHP的API_Php_Api_Curl - Fatal编程技术网

使用cURL将文件发送到带有PHP的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,

我正在尝试将文件上载到此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,
    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错误

提前谢谢


(对不起我的英语,我是法国人)

有两个潜在问题

  • 要使用cURL发布文件,它必须位于本地文件系统上。不能通过
    http://
    指定文件
  • 根据您的PHP版本,您可能需要使用该类,而不是在文件名前面加上
    @
  • 以下是修复这两个问题的一些代码:

    <?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上传图像。但不成功这里是我的代码