简体   繁体   English

使用php Curl使用laravel app文件夹中的文件向外部API发布请求

[英]Post request to external API with file from laravel app folder using php Curl

I am sending post API request to external server with a PDF file from my laravel app files folder which is under my public folder inside Laravel app folder. 我正在向我的laravel app files文件夹中的PDF文件发送后API请求到外部服务器,该文件夹位于Laravel app文件夹中的公共文件夹下。 i am sending PDF file from this folder and getting. 我正在从这个文件夹发送PDF文件并获取。 file not found error can someone help. 文件未找到错误可以有人帮忙。

$samplefile = "/var/www/html/laravel_project/public/1476505004.pdf";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://api_upload_usl/api/uploads");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,array('myFile'=>"@$samplefile",'token'=>$token,'id'=>1));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
curl_close($ch);

I'm not intamatley familiar with the way you're meant to upload files using curl, but as per this answer , 我不熟悉你使用curl上传文件的方式,但根据这个答案

Also, could you elaborate on file not found error ? 另外,你能详细说明file not found error吗? Are you sure that's a PHP error? 你确定这是一个PHP错误吗? Could it be the webserver telling you that the page you're requesting doesn't exist? 可能是网络服务器告诉您您要求的页面不存在吗?

You also didn't specify the curl as a post, maybe it's doing a different operation than the endpoint expects 您也没有将curl指定为帖子,也许它正在执行与端点期望不同的操作

EG: 例如:

if (function_exists('curl_file_create')) { // php 5.6+
  $cFile = curl_file_create($file_name_with_full_path);
} else { // 
  $cFile = '@' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

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

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