如何将保存的文件发送到Laravel中的外部API?

huangapple go评论65阅读模式
英文:

How to send saved file to external API in Laravel?

问题

以下是您要翻译的内容:

我正在构建一个NextJS网站应用,用户需要通过上传国民身份证来完成他的个人资料。我使用Laravel保存了上传的文件。一切都正常,直到保存部分。但当我尝试将已保存的文件发送到外部API时,API会抛出一个错误,要求我“发送有效的图像”。外部API接受JPG、JPEG和PDF格式。
这是我的代码:

$pan = Storage::get(auth()->user()->pan_photo);
$data = ['pancard' => $pan,];
$response = Http::asForm()
            ->withHeaders([
                '一些自定义标头'
            ])->put('https://staging.eko.in:25004/ekoapi/v1/user/service/activate', $data);
Log::channel('response')->info($response);

我在这里做错了吗?

英文:

I'm building a NextJS webapp in which a user is supposed to complete his profile by uploading his national ID. I'm saving the uploaded file using Laravel. Everything works fine till the saving part. But when I'm trying to send the saved to file to an external API, the API throws me an error asking me to Send a valid image. The external API accepts, JPG, JPEG and PDF.
Here's my code:

$pan = Storage::get(auth()->user()->pan_photo);
$data = ['pancard' => $pan,];
$response = Http::asForm()
            ->withHeaders([
                'some custom headers'
            ])->put('https://staging.eko.in:25004/ekoapi/v1/user/service/activate', $data);
Log::channel('response')->info($response);

Am I doing something wrong here?

答案1

得分: 0

你需要在这里使用 POSTPUT/PATCH 不会起作用,因为这是PHP的限制,所以不要使用 put 函数,改成 post

<?php 
Http::asForm()->post('你的网址在这里', $data);
英文:

You have to use POST here, A PUT/PATCH will not work as this is a limitation of the PHP, So instead of using put function you need to change it to post

&lt;?php 
Http::asForm()-&gt;post(&#39;Your url here&#39;, $data);

huangapple
  • 本文由 发表于 2023年3月21日 02:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794065-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定