Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel 5.5中通过卷发和接收过账_Php_Laravel - Fatal编程技术网

Php 在Laravel 5.5中通过卷发和接收过账

Php 在Laravel 5.5中通过卷发和接收过账,php,laravel,Php,Laravel,我试图在我的端点中使用cURL发布数据,该端点构建在Laravel上。在接收数据的API控制器中,我可以接收除媒体文件以外的所有数据。我使用$request->hasFile('file')检查文件是否存在,但它返回false。我还尝试使用$request->file('file')获取文件,但它返回null 当我使用$request->get('file')时,我得到以下响应 Laravel:5.5 PHP7.2.1 文件“{”name:“/Users/name/file/path/publi

我试图在我的端点中使用cURL发布数据,该端点构建在Laravel上。在接收数据的API控制器中,我可以接收除媒体文件以外的所有数据。我使用
$request->hasFile('file')
检查文件是否存在,但它返回false。我还尝试使用
$request->file('file')
获取文件,但它返回null

当我使用
$request->get('file')
时,我得到以下响应

Laravel:5.5

PHP7.2.1

文件“{”name:“/Users/name/file/path/public/media/aaaaah.wav”,“mime”:null,“postname”:null}

下面,我使用
$headers[]=“Content Type:application/json”
将收件人从数组转换为字符串。有人能帮助我理解为什么在我使用
$request->hasFile('file')
$request->file('file')
时,cURL发布的文件没有在我的Laravel方法中被接收到吗

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);
}
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')                 
        ]);
    }
}
我接收数据的端点:

APIController

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);
}
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')                 
        ]);
    }
}
响应

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

如果您正在发送文件,我建议您设置卷曲安全上载:

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
然后,您可能需要更改标题的内容类型并设置:

"Content-Type: multipart/form-data"
此外,我通常使用这种方法而不是
curl\u file\u create

if ( class_exists('CurlFile') ) {
  $file = new CurlFile('path-to-the-file', 'application/octet-stream');
} else {
  $file = '@' . realpath('path-to-the-file');
}

无论如何,我会说(但不是完全确定)代码的问题在于内容类型标题设置为JSON

使用Guzzle会容易得多:@ceejayoz,为什么您更喜欢Guzzle而不是Curl?因为它是专门为这类事情设计的,并且会自动处理进行Curl调用时的大多数常见错误。“有人能帮我理解为什么我的Laravel方法没有收到cURL发布的文件吗?”-这是因为您弄乱了内容类型。PHP没有从JSON提取文件上载数据的默认机制;您需要发送一个
多部分/表单数据
请求。@misorude,即使是
多部分/表单数据
,我仍然无法获取该文件。这实际上是我第一次尝试:)