简体   繁体   中英

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. i am sending PDF file from this folder and getting. 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 ,

Also, could you elaborate on file not found error ? Are you sure that's a PHP error? 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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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