2016-09-30 119 views
-1

我正在使用基本的PHP curl来发布数据到iNaturalist API call,但我得到内部服务器错误。PHP卷曲后调用上传其他属性的图像

我的HTML代码:

<!DOCTYPE html> 
    <html> 
    <body> 

    <form action="imageuploaddir.php" method="post" enctype="multipart/form-data"> 
     Select image to upload: 
     <input type="file" name="fileToUpload" id="fileToUpload"> 
     <input type="submit" value="Upload Image" name="submit"> 

    </form> 

    </body> 
    </html> 

和我imageuploaddir.php代码

<?php 
$authorization = "Authorization: Bearer ###somekey; 

$file=$_FILES["fileToUpload"]; 

$postfields = array('file' =>'@'.$file,'observation_photo[observation_id]'=> 4230809); 
var_dump($postfields); 
$ch =curl_init(); 
curl_setopt($ch,CURLOPT_URL,"http://www.inaturalist.org/observation_photos"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HEADER,FALSE); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); 
curl_setopt($ch, CURLOPT_VERBOSE,1); 
curl_setopt($ch, CURLOPT_POST,TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: multipart/form-data", 
    $authorization 
)); 
$response = curl_exec($ch); 
$err = curl_error($ch); 

curl_close($ch); 

if ($err) { 
    echo "cURL Error #:" . $err; 
} else { 
    echo $response; 
} 

?> 




$postfields = array('file' =>'@'.$file,'observation_photo[observation_id]'=> 4230809); 

当我给这个格式的文章中,我得到一个错误:

Notice: Array to string conversion

回答

0

我更改了postfields数组并将CURLOPT_SAFE_UPLOAD设置为false以成功将图像和其他属性发布到API。

$postfields = array(
     'file' => 
      '@'   . $_FILES['fileToUpload']['tmp_name'] 
      . ';filename=' . $_FILES['fileToUpload']['name'] 
      . ';type='  . $_FILES['fileToUpload']['type'] 
    ,'observation_photo[observation_id]'=>4230809); 

我还设置CURLOPT_SAFE_UPLOAD假curl_setopt($ CH,CURLOPT_SAFE_UPLOAD,FALSE);