使用Curl上传文件并以Laravel方法接收它们 [英] Upload file using Curl and receive them in Laravel method

查看:542
本文介绍了使用Curl上传文件并以Laravel方法接收它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用laravel应用程序终点将文件上传到使用curl.

I have been trying to upload file to using curl using laravel app end point.

下面的代码在单独的服务器上.

Below code is on separate server.

$header = $this->getCurlHeader();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);   
$cfile = new \CURLFile($this->filePath . $this->csvFile,  'text/csv','text.csv');
//print_r($cfile);exit;
$postData = array('csv' => $cfile);         
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);                    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$response = curl_exec($ch);
$hhtpCode = curl_getinfo($ch);
curl_close($ch);
//$this->dbg($hhtpCode);
$this->dbg(json_decode($response,1),1);

并且在不同服务器上托管的laravel应用中的接收端.

And receiving end in laravel app hosted on different server.

public function insert(Request $request, $feed=''){
     //return response()->json(var_dump($request->all()));
     //return response()->json("here in insert");
     return response()->json($_FILES);
     echo "here";
     dd($_FILES);
}

返回空响应. 我可以使用标头验证curl请求.

It returns me empty response. I am able to verify curl request using headers.

任何建议都会有所帮助. 谢谢.

Any suggestion will be helpful. Thanks.

推荐答案

您可以在源服务器上尝试类似的方法:

You can try something like this on the source server instead:

$target_url = 'https://mywebsite.com'; // Write your URL here
$dir = '/var/www/html/storage/test.zip'; // full directory of the file

$cFile = curl_file_create($dir);
$post = array('file'=> $cFile); // Parameter to be sent

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=json_decode(curl_exec($ch));
curl_close ($ch);

在目标服务器上,打印发送的请求.

From the destination server, print the request sent.

public function insert(Request $request){
    dd($request->file);
}

这篇关于使用Curl上传文件并以Laravel方法接收它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆