简体   繁体   English

使用CURL上传文件并使用Laravel方法检索

[英]Uploading File with CURL and Retrieving in Laravel Method

I'm trying to get data posted using cURL in my endpoint, which is built on Laravel. 我正在尝试使用cURL在基于Laravel构建的端点中发布数据。 In my API controller, where I receive data, I am able to receive all the data except my media file. 在我接收数据的API控制器中,我可以接收除媒体文件以外的所有数据。 I check for presence of the file using $request->hasFile('file') but it returns false. 我使用$request->hasFile('file')检查文件是否存在,但返回false。 I also try to get the file using $request->file('file') but it returns null. 我也尝试使用$request->file('file')但它返回null。

When I use $request->get('file') , I get the following response. 当我使用$request->get('file') ,得到以下响应。

file":{"name":"/Users/name/File/path/public/media/aaaah.wav","mime":null,"postname":null} 文件 “:{” 名 “:”/用户/名/文件/路径/公/媒体/ aaaah.wav”, “默”:空, “postname”:空}

Below, I am using $headers[] = "Content-Type: application/json"; 在下面,我使用$headers[] = "Content-Type: application/json"; to convert the recipient from array to string. 将收件人从数组转换为字符串。 Can anyone help me understand why the file posted by cURL is not being received in my Laravel method when I use $request->hasFile('file') and $request->file('file') ? 任何人都可以帮助我了解为什么在我使用$request->hasFile('file')$request->file('file')时在Laravel方法中未收到cURL发布$request->file('file')吗?

AppController AppController的

public function postCurlData()
{
   $endPoint = 'http://127.0.0.1:9000/api';          
   $apiKey = '****';
   $url = $endPoint . '?key=' . $apiKey;
   $dir = '/Users/name/File/path/app/public/media/test.wav'; // full directory of the file
   $curlFile = curl_file_create($dir);

    $data = [
       'recipient' => ['44909090', '44909090'],
       'content' => 'i love to code',
       'file' => $curlFile,          
     ];

    $ch = curl_init();
    $headers = array();
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    $result = curl_exec($ch);
    $result = json_decode($result, TRUE);
    curl_close($ch);
}

My endpoint where I'm receiving data: 我正在接收数据的端点:

APIController APIController

public function receiveCurlData()   
{   
    $apiKey = $request->get('key');
    if (($apiKey)) {
        return response()->json([
            'status' => 'success',
            'content' => $request->get('content'),
            'recipient' => $request->get('recipient'),
            'file' => $request->hasFile('file')                 
        ]);
    }
}

Response 响应

{"status":"success","content":"I love to code","recipient":
["44909090","44909090"],"file":false}

This answer is related to your question: how to upload file using curl with php . 这个答案与您的问题有关: 如何使用curl和php上传文件

You should delete 你应该删除

 $headers = array();
    $headers[] = "Content-Type: application/json";

And

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

And also delete the json_encode() replacing it by the plain array 并删除json_encode(),将其替换为普通数组

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    $result = json_decode($result, TRUE);
    curl_close($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM