首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将CURL命令转换为PHP以便与Laravel一起使用

将CURL命令转换为PHP以便与Laravel一起使用
EN

Stack Overflow用户
提问于 2017-07-17 14:54:04
回答 3查看 646关注 0票数 0

如何将下面的curl命令转换为Laravel使用的PHP?

代码语言:javascript
复制
curl -X POST -F "images_file=@fruitbowl.jpg" "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20"
EN

回答 3

Stack Overflow用户

发布于 2017-07-17 15:00:41

看看这个包,这是一个非常简单的laravel https://github.com/ixudra/curl卷曲包装器

所以看起来就像

代码语言:javascript
复制
   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
EN

Stack Overflow用户

发布于 2017-07-17 17:09:03

有一个用curl php来上传文件的功能

代码语言:javascript
复制
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;
}

用法:

代码语言:javascript
复制
$result = uploadFileWithCURL('path/to/your/fruitbowl.jpg','https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20');
票数 0
EN

Stack Overflow用户

发布于 2021-06-29 11:33:41

使用Http外观并调用attach方法,该方法接受文件名及其内容

代码语言:javascript
复制
use Illuminate\Support\Facades\Http;

$response = Http::attach(
    'images_file', '/home/user/fruitbowl.jpg', 'fruitbowl.jpg'
)->post('https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=xxxxa12345&version=2016-05-20'
)->json();

https://laravel.com/docs/8.x/http-client#multi-part-requests

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45137729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档