Laravel Response::download throws a FileNotFoundException exception when using URL::to - laravel-4

I am using Laravel 4.2 and want to make a download response using Response::download. So I used the following code:
$headers = array(
'Content-Type: application/vnd.android.package-archive',
);
return Response::download(URL::to("assets/install.apk"), "tracking.apk", $headers);
But I got a FileNotFoundException exception. Then, I found this answer and changed my codes to:
$headers = array(
'Content-Type: application/vnd.android.package-archive',
);
return Response::download(public_path() . "/assets/install.apk", "tracking.apk", $headers);
Now, it works. However, my question is what was wrong with URL::to?

Response::download expects a file system path such as /srv/http/some/file rather than an URL which would be http://hostname/some/file.
From the documentation :
Response::download($pathToFile);
You can also use optional parameters to set the name of the file that'll be saved on the client's side and an array additional headers like so :
Response::download($pathToFile, $name, $headers);

Related

A 'contents' key is required while uploading a file using Http client

I am struggling with a weird problem. I am trying to upload a file to a different server using the API. I am using Laravel 8.X and Using HTTP client for the same. Below is my code which I am calling from a controller
if ($request->hasFile('uploadReceipt') && $request->file('uploadReceipt')->isValid()) {
$receiptContent = file_get_contents($request->file('uploadReceipt'));
$originalName = $request->file('uploadReceipt')->getClientOriginalName();
$responseUploadReceipt = Http::attach('attachment', $receiptContent, $originalName)
->withHeaders([
'Accept'=> 'application/json',
'Authorization' => 'Bearer '.$userAccessToken
])
->post($endpoint, $requestData);
dd($responseUploadReceipt->json());
} else {
dd ("Else");
}
I am getting A 'contents' key is required, in the file vendor/guzzlehttp/psr7/src/MultipartStream.php:86
I spent almost 12 hrs on it but unable to resolve this issue. I also tried with the stream like
$receiptContent = fopen($request->file('uploadReceipt'), 'r');
but the same issue. I followed the same way it is mentioned in the documentation. Anyone who can help me with this will be appreciated.
Thanks and Regards
Ashish
You were probably using multipart in conjunction with form_params , this is not explicitly explained in the documentation of laravel but guzzle won't work with both
Note
multipart cannot be used with the form_params option. You will need to
use one or the other. Use form_params for
application/x-www-form-urlencoded requests, and multipart for
multipart/form-data requests.
This option cannot be used with body, form_params, or json
To solve this problem you will need to parse all the params to multipart, if you are using laravel or lumen you can do it in this way
if(!empty($this->files))
{
//if there is an image parse all the rest parameters to
multipart
$file_keys=array_keys($this->files);
foreach($this->files as $k => $file)
{
$http = $http->attach($k, file_get_contents($file),$k);
}
foreach($this->data as $dk =>&$d)
{
if(!in_array($dk,$file_keys))
{
if(is_array($d))
{
$d=json_encode($d);
}
$http = $http->attach($dk,$d);
}
}
//if there isn't any file just send all as form_params
return $http=$http->post($this->url);
}
return $http=$http->post($this->url,$this->data);
https://docs.guzzlephp.org/en/stable/request-options.html#multipart
I think this line is the problem:
$receiptContent = file_get_contents($request->file('uploadReceipt'));
The examples in the documentation do not use the Request. $request->file('uploadReceipt') should return an instance of Illuminate\Http\UploadedFile and I'm pretty sure file_get_contents doesn't work with an UploadedFile as a parameter.
Digging deeper into the class file, it seems there is a method to get the UploadedFile's contents:
Illuminate\Http\UploadedFile
/**
* Get the contents of the uploaded file.
*
* #return false|string
*
* #throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function get()
{
if (! $this->isValid()) {
throw new FileNotFoundException("File does not exist at path {$this->getPathname()}.");
}
return file_get_contents($this->getPathname());
}
Try $receiptContent = $request->file('uploadReceipt')->get(); instead.

Laravel 5.2 HTTPNOTFOUNDEXCEPTION

I cant figure out what is causing this error. I have checked to see if the parameters are correct and they seem to be. Also if anybody has an alternative way to get all the parameters in the route rather than listing them all please do tell. I can't find another way.
public function savePaymentDetails(Request $request, $code, $message, $mPAN, $type, $exp, $name, $TxnGUID,
$ApprovalCode, $CVVMatch, $GT_MID, $GT_TRANS_ID, $GT_Val_Code, $ProcTxnID,
$session, $card_brand_selected, $CRE_Verbose_Request, $CRESecureID,
$trans_type, $content_template_url, $allowed_types, $order_desc, $sess_id,
$sess_name, $return_url, $total_amt, $submit, $ip_address, $customer_lastname, $customer_firstname)
{
$code2 = $request->get('code');
echo $code2;
echo $code;
}
The route
Route::get('return/{code}/{message}/{mPAN}/{type}/{exp}/{name}/{TxnGUID}/{ApprovalCode}/{CVVMatch}/{GT_MID}/{GT_Trans_Id
}/{GT_Val_Code}/{ProcTxnID}/{session}/{card_brand_selected}/{CRE_Verbose_Request}/{CRESecureID }/{trans_type}/{content_template_url}/{allowed_types}/{order_desc}/{sess_id}/{sess_name}/{return_url}/{total_amt
}/{submit}/{ip_address}/{customer_lastname}/{customer_firstname}', 'PaymentController#savePaymentDetails');
Here are the parameters returned by the URL that I need to get
/return?code=000&message=Success&mPAN=XXXXXXXXXXXX1111&type=Visa&exp=1218&name=test+visa&TxnGUID=6041323& ApprovalCode=VI0151&CVVMatch=M&GT_MID=672840408068703&GT_Trans_Id=016142173277748&GT_Val_Code=AACA&ProcTxnID=6041323&session=e91dd8af53j35k072s0bubjtn7&card_brand_selected=Visa&CRE_Verbose_Request=1&CRESecureID=gt153545888233SB&trans_type=+2&content_template_url=https%3A%2F%2Fexample.com%2Fpublic%2Ftemplate&allowed_types=Visa|MasterCard|American+Express&order_desc=6&sess_id=e91dd8af53j35k072s0bubjtn7&sess_name=session&return_url=https%3A%2F%2Fexample.com%2Fpublic%2Freturn&total_amt=1.51&submit=submit&ip_address=10.108.231.98&customer_lastname=visa&customer_firstname=test
The route rule you define actually match the url like this
code/message/mPAN/type/exp/name/TxnGUID/ApprovalCode/CVVMatch/GT_MID/GT_Val_Code/ProcTxnID/session/card_brand_selected/CRE_Verbose_Request/trans_type/content_template_url/allowed_types/order_desc/sess_id/sess_name/return_url/submit/ip_address/customer_lastname/customer_firstname
instead of
return?code=blab&blablabal=blab
You url dismatch any route you define , so a not found exception was thrown .
If you are trying to get all url parameters you can just write your route like that .
Route::get('return', 'PaymentController#savePaymentDetails');
And get parameters in your controller :
$parameters = $requests->all();
What's more , if you are trying to save something to your database , you'd better use the post method , you can find more When should i use post or get.

Laravel 5: Calling routes internally

Is there a way, in Laravel 5, to call routes internally/programmatically from within the application? I've found a lot of tutorials for Laravel 4, but I cannot find the information for version 5.
Using laravel 5.5, this method worked for me:
$req = Request::create('/my/url', 'POST', $params);
$res = app()->handle($req);
$responseBody = $res->getContent();
// or if you want the response to be json format
// $responseBody = json_decode($res->getContent(), true);
Source:
https://laracasts.com/discuss/channels/laravel/route-dispatch
*note: maybe you will have issue if the route you're trying to access
has authentication middleware and you're not providing the right credentials.
to avoid this, be sure to set the correct headers required so that the request is processed normally (eg Authorisation bearer ...).
UPDATE: i've tried this method with laravel 8 and it works but if you're using PHP version 8.0 you might need to call opcache_reset(); before this line $req = Request::create('/my/url', 'POST', $params); to avoid an error.
see guzzlehttp/guzzle dosn't work after update php to php 8 for more info
You may try something like this:
// GET Request
$request = Request::create('/some/url/1', 'GET');
$response = Route::dispatch($request);
// POST Request
$request = Request::create('/some/url/1', 'POST', Request::all());
$response = Route::dispatch($request);
You can actually call the controller that associates to that route instead of 'calling' the route internally.
For example:
Routes.php
Route::get('/getUser', 'UserController#getUser');
UserController.php
class UserController extends Controller {
public function getUser($id){
return \App\User::find($id);
};
}
Instead of calling /getUser route, you can actually call UserController#getUser instead.
$ctrl = new \App\Http\Controllers\UserController();
$ctrl->getUser(1);
This is the same as calling the route internally if that what you mean. Hope that helps
// this code based on laravel 5.8
// I tried to solve this using guzzle first . but i found guzzle cant help me while I
//am using same port. so below is the answer
// you may pass your params and other authentication related data while calling the
//end point
public function profile(){
// '/api/user/1' is my api end please put your one
//
$req = Request::create('/api/user/1', 'GET',[ // you may pass this without this array
'HTTP_Accept' => 'application/json',
'Content-type' => 'application/json'
]);
$res = app()->handle($req);
$responseBody = json_decode($res->getContent()); // convert to json object using
json_decode and used getcontent() for getting content from response
return response()->json(['msg' =>$responseBody ], 200); // return json data with
//status code 200
}
None of these answers worked for me: they would either not accept query parameters, or could not use the existing app() instance (needed for config & .env vars).
I want to call routes internally because I'm writing console commands to interface with my app's API.
Here's what I did that works well for me:
<?php // We're using Laravel 5.3 here.
namespace App\Console;
use App\MyModel;
use App\MyOtherModel;
use App\Http\Controllers\MyController;
use Illuminate\Console\Command;
class MyCommand extends Command
{
protected $signature = 'mycommand
{variable1} : First variable
{variable2} : Another variable';
public function handle()
{
// Set any required headers. I'm spoofing an AJAX request:
request()->headers->set('X-Requested-With', 'XMLHttpRequest');
// Set your query data for the route:
request()->merge([
'variable1' => $this->argument('variable1'),
'variable2' => $this->argument('variable2'),
]);
// Instantiate your controller and its dependencies:
$response = (new MyController)->put(new MyModel, new MyOtherModel);
// Do whatever you want with the response:
var_dump($response->getStatusCode()); // 200, 404, etc.
var_dump($response->getContent()); // Entire response body
// See what other fun stuff you can do!:
var_dump(get_class_methods($response));
}
}
Your Controller/Route will work exactly as if you had called it using curl. Have fun!

response::download is doing nothing

I am having problems getting files to download outside of the public folder. Here is the Scenario:
These files cannot be accessed from anywhere but through this application and these downloads must go through access control. So a user can't download the file if they are not logged in and have permission to.
I have a route defined with a get variable. This ID goes into the controller and the controller calls the method below:
public static function downloadFile($id){
$file = FileManager::find($id);
//PDF file is stored under app/storage/files/
$download= app_path().substr($file->location,6);///home/coursesupport/public_html/app/storage/files/fom01/7-2/activities-and-demos.pdf
$fileName = substr($download,strrpos($download,'/')+1);//activities-and-demos.pdf
$mime = mime_content_type($download);//application/pdf
$headers = array(
'Content-Type: '.$mime,
"Content-Description" => "File Transfer",
"Content-Disposition" => "attachment; filename=" . $fileName
);
return Response::download($download, $fileName, $headers);
}
the problem is it does nothing, just opens a blank page. Am I missing something? oh yeah, and the link to the route mentioned above opens a blank tab.
I appreciate any help. Thanks!
It should work by doing only this:
public static function downloadFile($id)
{
$file = FileManager::find($id);
$download= app_path().substr($file->location,6);
$fileName = substr($download,strrpos($download,'/')+1);
return Response::download($download, $fileName);
}
Make sure your route is actually returning the returned response. Eg:
Route::get('download', function()
{
return DownloadHandler::downloadFile(1);
});
Without the return in the route the IlluminateResponse will do nothing.

Curl Delete function dumps json to browser - how to fix

The following function transfers a curl delete request to an api, and returns the response to php.
The api returns a json array for all requests (get, post, put and delete), and everything works fine for everything except delete requests.
The following curl function doesn't seem to be working properly:
function curl_delete($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
return $result; // <-- Function ALWAYS returns whether this is here or not??!..
}
This is how I call the function from PHP:
// [DELETE API URL]:
$url = 'http://localhost/website/api/users/user/id/123';
$html = json_decode(curl_delete($url), true);
If you look at the function (above), whether or not I include the return statement (at the end of the function), the result of the curl call (the jsonified array) always gets dumped to the browser after the function has completed - which is not what I want.
If I then say echo count($html), the correct length of the returned result will print, and the $html array is working fine, however I can't seem to prevent it from automatically dumping to the screen.
This does not happen for any of the other curl functions, which just work as expected.
QUESTION:
Is this normal behaviour? How do I prevent the json from dumping to the screen?
PS Using Codeigniter & Phil Sturgeon's REST Server
You should try Phil Sturgeon's cURL library for Codeigniter and its method simple_delete() :
$this->load->library('curl');
$url = 'http://localhost/website/api/users/user/id/123';
$response = $this->curl->simple_delete($url);
$html = json_decode($response, TRUE);
If it does not solve your problem, maybe you will have to take a closer look at your REST server.

Resources