2017-07-19 56 views
0

我曾嘗試使用CURL呼籲發送圖像文件API。但我得到以下錯誤:捲曲狀態碼415是否有任何PHP.ini設置?

"statusCode":415, 
"error":"Unsupported Media Type" 

請幫助我。

我重視代碼在這裏:

$filename = "screenshot.jpg"; 
$handle = fopen($filename, "r"); 
$xml = fread($handle, filesize($filename)); 
fclose($handle); 
$jwt_token = "xxx"; 
$authorization = "Authorization:".$jwt_token; 
$url = "https://prod0-commerce-api.sprinklr.com/media_upload"; 
$headers = array(    
          "Content-Type: image/jpg]", 
          "Cache-Control: no-cache", 
          "Pragma: no-cache", 
          $authorization 
          ); 
$postdata = array('fileName' => '@'.$filename, 
      'type' => 'IMAGE'); //<------------- 

     $soap_do = curl_init(); 
     curl_setopt($soap_do, CURLOPT_URL,   $url); 
     //curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 60); 
     //curl_setopt($soap_do, CURLOPT_TIMEOUT,  60); 
     curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false); 
     //curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); 
     curl_setopt($soap_do, CURLOPT_POST,   true); 
     curl_setopt($soap_do, CURLOPT_POSTFIELDS,  $postdata); //<----------- 
     curl_setopt($soap_do, CURLOPT_HTTPHEADER,  $headers); 

     $result = curl_exec($soap_do); 
     // Check for errors and display the error message 
     curl_close($soap_do); 
     $httpcode = curl_getinfo($soap_do, CURLINFO_HTTP_CODE); 
     echo $httpcode; 
echo "<pre>"; 
var_dump($result); 
if($errno = curl_errno($soap_do)) { 
    $error_message = curl_strerror($errno); 
    echo "cURL error ({$errno}):\n {$error_message}"; 
} 
echo "string";exit(); 
     print_r($result); 

回答

1

的問題是在此塊:

$postdata = array('fileName' => '@'.$filename, 
        'type' => 'IMAGE'); //<------------- 

type值應該是一個有效的MIME type(又名「媒體類型」或「內容類型」 )。

MIME類型是由斜槓(/)連接的兩部分(類型和子類型)組成的標識符。

該類型標識內容類別(文本,圖像,音頻,視頻,應用程序等)。子類型標識了類別內的更精確的內容。

對於圖像類型是image有幾個亞型:gifjpegpng

正確的MIME類型的圖像文件看起來像image/jpegimage/png並不僅僅是IMAGE。這就是服務器拒絕你的查詢的原因。

PHP函數getimagesize()可用於查找存儲在文件中的圖像的MIME類型。

您的代碼應該是這樣的:

$imgInfo = getimagesize($filename); 
$postdata = array('fileName' => '@'.$filename, 
        'type' => $imgInfo['mime']); 

而且沒有,有在php.ini爲你寫正確的代碼沒有設置。

+0

API只允許3種類型值IMAGE,VIDEO,FILES –

0

大多數的時候,你沒有正確設置Content-Type的一個錯誤415出現。也許你可以把內容類型:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpg")); 
+0

我已經設置 「內容類型:application/JSON;字符集= \」 UTF-8 \ 「」, –

+0

如果我設置爲u建議我得到這個錯誤「無效內容類型報頭「 –

+0

你能發送代碼嗎? –

0

I have solved using this solution

<?php 

$file = "http://localhost/xxx/screenshot.jpg"; 
$boundary = md5(time()); 
$eol = "\r\n"; 
$params = "----".$boundary.$eol 
    . "Content-Disposition: form-data;name=\"type\"".$eol 
    . $eol 
    . "IMAGE" 
    . $eol 
    . "----".$boundary.$eol 
    . "Content-Disposition: form-data;name=\"file\"; filename=\"screenshot.jpg\"".$eol 
    . '"Content-Type: image/jpeg\"'.$eol 
    . $eol 
    . file_get_contents($file) .$eol 
    . "----".$boundary."--"; 

$jwt_token = "xxxx"; 
$authorization = "Authorization:".$jwt_token; 

$first_newline  = strpos($params, $eol); 
$multipart_boundary = substr($params, 2, $first_newline - 2); 
$request_headers = array(); 
$request_headers[] = $authorization; 
$request_headers[] = 'Accept: application/json'; 
$request_headers[] = 'Content-Length: ' . strlen($params); 
$request_headers[] = 'Content-Type: multipart/form-data; boundary='. $multipart_boundary; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'xxx'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
$result = curl_exec($ch); 
$info = curl_getinfo($ch); 


curl_close($ch); 
    echo "<pre>"; 
print_r($result); 

exit();