2017-07-17 213 views

回答

0

看看這個包,這是laravel https://github.com/ixudra/curl

這樣一個超級簡單的捲曲包裝它看起來像

use Ixudra\Curl\Facades\Curl; 

    $response = Curl::to('https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20') 
    ->withContentType('multipart/form-data') 
    ->withData(['images_file' => file_get_contents("yourfile")]) 
    ->containsFile() 
    ->post(); 
+0

我得到語法錯誤,意外的' - >'(T_OBJECT_OPERATOR) - > withData行 – DanielA

+0

是的,它錯過了一個大括號,我將數組轉換爲短語法,所以它更清晰對不起:) –

0

還有就是上傳文件中的函數curl php

function uploadFileWithCURL($fullFilePath, $targetURL) 
{ 
    if (function_exists('curl_file_create')) { 
     $curlFile = curl_file_create($fullFilePath); 
    } else { 
     $curlFile = '@' . realpath($fullFilePath); 
    } 
    $post = array('file_contents' => $curlFile); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $targetURL); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $result = curl_exec($ch); 
    curl_close($ch); 

    return $result; 
} 

用法:

$result = uploadFileWithCURL('path/to/your/fruitbowl.jpg','https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20');