How to run an independent PHP script in Laravel? - php

I am new to laravel and PHP, so I apologize if something similar is asked before which I couldn't find as per my needs. I have an independent PHP script in public folder:
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','fyp');
define('API_ACCESS_KEY', 'AAAAk4V5b8k:APA91bFUo5crCI-gmfhlB9znAWpxdy9bdvA-29VBgLdRmQtYvjNuUe4xi7lkmyAmTOuhuCaaSphaGTvULTq6oREJEUwNNRBW9AKiadl9zzjbXFnEwINlfJzGFIpZojWim2cTT2D7rKJz');
//$registrationIds = 'f4tul16ETjK6wR64Nix7HO:APA91bGPa4ijApEp_vrDcAr-NEzUU-WcovjLhpxtwSiNv94aZ-aOMVek0AbTHNFHCmwZrXOzeqocFglWOlABWcjcDXnDdmh2qEsIgh0X7MkXcN_YD6qBREP5QyS1WlGn27qGiyYTdpoV';
$id = $_POST['id'];
$status = $_POST['status'];
$case_id = $_POST['case_id'];
What I want is to pass the values to this script and execute when I press a button on my page e.g.
When I press save, the following function in the controller executes:
public function storedetail(Request $req, $id)
{
$detail = Detail::firstOrNew(['case_id' => $id,]);
$detail->lawyerName = $req->input('lawyerName');
$detail->event = $req->input('proc_type');
$detail->room = $req->input('room');
$detail->eventDate = $req->input('date');
$detail->status = 1;
// $data = Cases::find($id);
$detail->case_id = $id;
// $data->details()->save($detail);
$detail->save();
return redirect()->back();
}
What is the simplest way to pass the id, status and case_id to the PHP script and execute it?

As per I understand you just need to pass data from laravel controller to separate php file.
just pass all the parameters to the specific file path.
ex : xyz.com/public/phpfile.php?id=123&status=abc&case_id=45

Related

Why is necessary two requests to transfer the file?

I have this method below to download a file from an API server. But its not working properly, when the user clicks in the button that calls the getFile() method it appears at first in the page the error
Trying to get property of non-object
But if the user click in the browser refresh button the file is downloaded.
So it seems that for the file be generated is necessary send two requests and in the first appears
Trying to get property of non-object
In the second the file is transfered, in the third it appears again
Trying to get property of non-object
In the API says that to download the file is an asynchronous operation, which means that the file may not be ready immediately. I dont know if the issue can be because of that.
Code:
public function getFile($regId){
$client = new \GuzzleHttp\Client();
$user = Auth::user();
$registration = $user->registrations()->with(["proforma"])->where("id", $regId)->first();
$proforma = $registration->proforma->proforma_number;
$getProforma = $client->request('GET', 'https://...'.$proforma.'.json', [
'query' => ['api_key' => '...'],
]);
$response = $getProforma->getBody()->getContents();
$url = null;
if(!empty($response)) {
$response = json_decode($response);
$url = !empty($response->output->pdfUrl) ? $response->output->pdfUrl : '';
}
header("Location: $url");
}
public function getFile($regId)
{
$registration = auth()->user()->registrations()->with(["proforma"])->where("id", $regId)->first();
$proforma = $registration->proforma->proforma_number;
$getProforma = json_decode(file_get_contents('https://...' . $proforma . '.json?api_key=.....'));
// $getProforma->output here was your problem so I advice you to: dd($getProforma);
$url = ! empty(optional($getProforma->output)->pdfUrl) ? optional($getProforma->output)->pdfUrl : '/'; // it will redict home if it couldn't find the PDF URL
return redirect($url);
}

Global PHP Database File

I'm currently making a PHP script and I need some help. Firstly it allows the user to enter database information on a file called install.html which presents a form to the user. The form uses GET to then send that information to a second install file which creates the relevant tables, enters the information into the tables and then allows the user to carry on with the script.
However I was wondering. In the second install file I used:
$databaseServer = $_GET["databaseServer"];
in order to get the information that was entered into the form. Is there anyway I can then send these variables ($databaseServer, $databaseName, $databaseUser, $databasePassword) to another file called db.php that I will include on top of every file I write that requires an SQL connection. I have looked at GLOBAL variables but they didn't work properly. I could have been doing something wrong however.
You could save a configuration array to a file:
<?php
class Config
{
public $path;
public function __construct($path)
{
$this->path = $path;
}
public function store($config)
{
$dump = var_export($config, true);
$dump = '<?php return ' . $dump . ';';
file_put_contents($this->path, $dump);
}
public function retrieve()
{
return include $this->path;
}
}
// Build your config array
$config['database'] = $_GET['database'];
$config['username'] = $_GET['username'];
// Make sure your server can write to this path
$configurator = new Config(__DIR__ . '/config/config.php');
// Save your config
$configurator->store($config);
// Get your config later
$read_config = $configurator->retrieve();
// Check our config against the saved version
assert($config == $read_config);
var_dump($read_config);

Testing 2 consecutive client requests in Symfony2

In Symfony2, I want to test a controller action with 2 subsequent requests if it behaves properly.
The first request will analyze the database and take appropriate action, the second request will also analyze the database and take a different action.
My code goes as follows:
protected function setUp() {
$this->_client = static::createClient();
$kernel = static::$kernel;
$kernel->boot();
$this->_em = $kernel->getContainer()->get('doctrine.orm.default_entity_manager');
$this->_em->beginTransaction();
}
public function testAddToCartWith2Posts() {
$this->addObjects(); // Initialize the database
$object = static::$kernel->getContainer()->get('doctrine')->getRepository('BaseBundle:Object')->findAll()[0];
$id = $object->getId();
$crawler = $this->_client->request('POST', '/cart/add/' . $id);
$crawler = $this->_client->request('POST', '/cart/add/' . $id);
$session = static::$kernel->getContainer()->get('session');
$cart = $session->get('cart');
$this->assertEquals($session->getId(), $cart->getSession());
$this->assertEquals(2, count($cart->getCartItems()));
}
The first request is able to read the list of objects. The second request is not.
The database becomes empty between requests. How could I fix this problem?

PHP OOP function won't grab data unless another function is called

I'm having this weird problem right now, I'm just working on a small admin backend thing that will allow users to upload a file and download it again, simple stuff.
I'm using PHP OOP with Classes and functions and things. Still pretty new at it. Currently on one page I have a "getRecentLinks" function that will call information from a couple tables and put it all out on the page in a table, it works just fine. One of the options on this page is to download the file that was uploaded to that individual row. So I just want it to be a link you click on to say, file-download.php?id=3.
Now on file-download.php I'm currently just testing to get the information on the page before I add the headers and things in there. So I just have a simple
$l = new Links();
$file = $l->getFileInfo($_GET['id']);
print_r($file);
this SHOULD just return information from the database, id, name of file, size, data, and mimetype.
Now this doesn't work. I have no idea why but it doesn't.
Now on my page where I have the getRecentLinks() function it works just fine. I even brought in the getRecentLinks() into my file-download.php page so it's setup like this.
$l = new Links();
$l->getRecentLinks();
$file = $l->getFileInfo($_GET['id']);
print_r($file);
This works just fine and dandy, The second I remove getRecentLinks() it stops calling information from getFileInfo() and I cannot figure it out. I mean it's not a huge deal i could just keep #l->getRecentLinks() there but I can see this getting annoying if I have to add it to every page I want to do something, I'm just at the start of this project.
Here's the code in the Links class
public function getFileInfo($id)
{
$result = $this->runQuery("SELECT * FROM file WHERE id ='".mysql_real_escape_string($id)."'");
$resultSet = $this->fetch($result);
return $resultSet;
}
and
public function getRecentLinks()
{
$result = $this->runQuery("SELECT * FROM links ORDER BY date DESC");
while($j = $this->fetch($result))
{
$resultSet[] = $j;
}
return $resultSet;
}
And heres my connections and fetch functions a friend helped me develop
public function runQuery($sql) {
$this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
if(!$this->connection) {
die("MySQL is down.");
} else {
mysql_select_db($this->dbname);
}
$result = mysql_query($sql,$this->connection) or die(mysql_error());
return $result;
}
public function fetch($result)
{
$resultSet = mysql_fetch_assoc($result);
return $resultSet;
}
mysql_real_escape_string requires a connection. Since getRecentLinks opens a connection it works. But you call mysql_real_escape_string before runQuery (in execution order) in getFileInfo. In this case, I assume the id is an integer, so you're better off and a lot cheaper to call intval($_GET['id']).

Passing parameters from an outside file to a class

I need to have two files.
Let's call the file controlling a second one: main.php and let's call the secondary file, the one with my code: second.php
In second.php I have:
<?php
class tags{
var $theuser;
var $thebarname;
var $theplace;
var $thetime;
var $themail;
function __construct($theuser,$thebarname,$theplace, $thetime, $themail){
$this->theuser=$theuser;
$this->thebarname=$thebarname;
$this->theplace=$theplace;
$this->thetime=$thetime;
$this->themail=$themail;
}
function give_tags_theuser(){
return $this->theuser;
}
function give_tags_thebarname(){
return $this->thebarname;
}
function give_tags_theplace(){
return $this->theplace;
}
function give_tags_thetime(){
return $this->thetime;
}
function give_tags_themail(){
return $this->themail;
}
}
$tags = new tags("John", "Starbucks", "NY", "4:30", "example#example.com");
$user= $tags->give_tags_theuser();
$barname = $tags->give_tags_thebarname();
$place = $tags->give_tags_theplace();
$time = $tags->give_tags_thetime();
$email = $tags->give_tags_themail();
// with the data before I will send an email using phpmailer, but that's another story
?>
In main.php I need to pass the variables to the class. Meaning that I will delete:
$tags = new tags("John", "Starbucks", "NY", "4:30", "example#example.com");
from second.php and I will be passing this data from main.php
What do I have to change in second.php and how would main.php be in order to do so?
If I did not explain myself, please tell me so, I'm still a student and I'm probably still messing up with the vocabulary.
Thanks a lot
If your using classes then you shouldn't really be doing operations on those classe inside the files that define those classes.
For example given your situation you should define the tags class inside a file called tags.php then make your main.php file the runner of your application, so in that file do:
require_once 'tags.php'
$tags = new tags("John", "Starbucks", "NY", "4:30", "example#example.com");
$user= $tags->give_tags_theuser();
$barname = $tags->give_tags_thebarname();
$place = $tags->give_tags_theplace();
$time = $tags->give_tags_thetime();
$email = $tags->give_tags_themail();
This is better than writing code which runs your application in multiple files.

Categories