I want to pass data from one application to another in Laravel..
Suppose I want to take user data from one application and I want to send this data to another application form.
I wanted to send it this way..But I'm getting Error..
So if anyone could suggest any solution for this..would be appreciated ..
public function store(Request $request)
{
$payment = new Payment();
$payment->username = $request->Input(['username']);
$payment->price = $request->Input(['price']);
$payment->purchase_id = $request->Input(['purchase_id']);
$payment->save();
$store_id =\Hash::make($payment->id);
$price = $payment->price;
return \Redirect::to('http://localhost/blog/public/getPayment');
}
And I get the following Error:
InvalidArgumentException in Response.php line 462:
The HTTP status code "0" is not valid.
If you just want your user to be redirected to another app so they can fill whatever forms you can use
return Redirect::away('http://localhost/blog/public/getPayment');
See the method here.
BUT
If you want to send data to another app without redirecting the user, the best solution I can give is to make an API on this other app so you can make requests (with Guzzle or something else) to this API, allowing you to pass some data.
Related
to me what is needed to prevent outsiders. I just want to respond to requests from those places I have defined.
Example : allowed applications and url
com.x.app
com.y.app
--------- AND ---------
http://www.x.com
Is there an easy way to do this? Best regards!
You should explain what you want clearly. Basically, to do something like this you need to check the $_SERVER['HTTP_USER_AGENT'] attribute of the HTTP header that you are receiving and filter on Android or iPhone keywords.
The best way to build a RESTful API in Laravel is via the routes/api.php
When you add a route to this file, such as the following:
Route::get('/users/list', 'ApiController#userList');
This means when you go to yourwebsite.com/api/users.list, it will execute the given method in the given controller.
As far as authenticating you API users, you can store your users and their API keys in a database and authenticate them before they reach the method by using the magic construct method.
public function __construct() {
$inputKey = filter_input(INPUT_GET, 'key'); //filter input the way you want
if(!isCustomer($inputKey) { //validate api keys in your app somehow
die('Authentication Failure'); //kick the freebooters
}
}
I have a laravel api controller where I'm getting data from a table.
public function moveData() {
$movelivesales = DB::table('st_sales_live')->get();
return $movelivesales;
}
I'm getting all the data, now how can I send this data to another project api(this api is ready) using request ? Any help please?
Just create a route like this to get your data($movelivesales):
Route::get('/your/path', 'YourController#moveData');
Then if you access:
http://yourdomain.com/your/path you will get a json of your data
You can get the data from api with php: file_get_contents('http://yourdomain.com/your/path');
I am developing a laravel api and sucessfully made gets, posts, deletes and i am now trying to do the update method using PATCH.
Here is my first try:
public function update($id, Request $request)
{
$taxes = taxes::find($id);
$taxes ->fill($request->only(['$request']));
$taxes->save();
}
And testing it with the follow url on postman
****.local/api/taxes/1?description=test
This obviously doesnt work, i tought i would be able to see the error but i am currently on a different machine but hopefully someone can guide me to correct path to make this update method.
Dont forget its an api, no views/forms.
Thank you.
You have to specify the body of the query (so the variables you want to change) in the x-www-form-urlencoded tab of the Body tab for your request in Postman.
You can find a screenshot here: http://img11.hostingpics.net/pics/987644Sanstitre1.jpg (waiting for imgur to be back online)
You also have an issue in your method, your code should be the following:
public function update($id, Request $request)
{
$taxes = taxes::find($id);
$taxes ->fill($request->only(['description', 'anyotherfield', '...']));
$taxes->save();
}
Could be to do with this line: $request->only(['$request']), as your request is unlikely to have a parameter called $request. You might've been meaning to use $request->all() (all input) or $request->only('description', 'etc.') which will whitelist given paramters.
Probably should give your model a singular name, and capitalize it.
Tax::findOrFail($id)->update($request->only('description'))
I have a ionic application which sends json data to my api which uses laravel as it's backend.
When the post request is made to my route, I call a method on my controller..
This controller then takes the data and enters it into the database, however sometimes not all the data sent from the ionic application maybe there.
I.e. My laravel application is waiting for First Name, Last Name, Company and sometimes my ionic application will only send off first_name, last_name meaning the index for company is missing.
How do I in my controller check if an index exists?
See my json request:
{"data":{"first_name":"Peter","last_name":"tESt"}}
See my controller waiting the data request:
public function store(Request $request)
{
$card = NEW card;
$card->first_name = $request->data['first_name'];
$card->last_name = $request->data['last_name'];
$card->location = $request->data['location'];
$card->save();
};
In some cases the location will be included and in some cases it won't be. The application won't send a empty index either.
In PHP, you can check if a key exists in an array using array_key_exists
In your example, you could do something like:
if (array_key_exists('location', $request->data)) {
$card->location = $request->data['location'];
}
I am building a mobile app (iOS) and Symfony2 REST backend. On Symfony2, my routes are working correctly and I have tested them with AJAX and httpie, all CRUD operations, etc are fine. Now, I am trying to access the routes from the app. So far, I can access the routes and when I look into the Symfony2 Profiler, I can see entries in last 10 entries to verify that I am hitting the server with my POST and GET requests. Now, I have 2 questions and I would be glad if people can point me in the direction for ** Best Practices ** on how to proceed.
Problem 1: Although I am posting data which I can see coming in under "Request", when I try to create a record, it creates only NULL records, meaning the data is being lost. This is my controller for creating users for example:
public function postUserAction(Request $request)
{
$content = $this->get('request')->getContent();
$serializer = $this->get('jms_serializer');
$entity = $serializer->deserialize($content, 'Name\BundleName\Entity\User', 'json');
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return array(
'entity' => $entity,
);
}
When I look into the log, the only things that stand out are: Request Cookies (No cookies), Request Content: "Request content not available (it was retrieved as a resource)." This tells me the data was missing, how can I get this data and use it? Or what else could it be?
Problem 2: GET returns an empty JSON response with no data just the keys when I NSlog (echo it). My code looks like:
public function getUsersAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('NameBundle:User')->findAll();
return array(
'entities' => $entities,
);
}
From the log, it has the Request Cookies set: PHPSESSID => "1udududjjs83883jdlb4ho0j4" but again the Request Content says: "Request content not available (it was retrieved as a resource)." How can I make it return the data with the JSON? This works well in the browser AJAX and httpie tests.
Problem 3: Using AFNetworking, I have a symbolic constant which I set as the APIHost (IP Address) and APIPath was the folder. Now in my earlier version using native PHP, I constructed the actual code to be executed in index.php by sending the parameter in JSON so if I wanted a login, I sent something like todo:login but with Symfony2, I am not sure or know even the best practices for this case. Ideally, I would like to specify the server-side request in the JSON request and then find the correct route in Symfony2 but is this how to do it and if yes, can you please provide an example? The workaround is to specify hard coded paths in AFNetworking each time I need to make a request which I think tightly couples the code and I need to make changes in a lot of places anytime something changes on the server side. Thanks and sorry for the long question!
You expect the jmsserializer to do magic for you. But it won't, you have to configure it first. From you code I can see that you are using jmsserializer wrong.
In getUsersAction() you have to return a serialized response, but you are returning an array of objects. This would be the right way:
public function getUsersAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('NameBundle:User')->findAll();
$serializer = $container->get('jms_serializer');
return array(
'users' => $jsonContent = $serializer->serialize($entities, 'json');,
);
}
Your post action basically looks ok, however when the json does not contain every field of entity USER the deserialization will fail. You can configure the entity for serialization/deserialization using annotations.
http://jmsyst.com/libs/serializer/master/reference/annotations
I am not sure if I understood your last problem, but I think you have to hardcode the path in your app.
Symfony2 is great and absolutely useful when writing an API. But if you don't want to deal with serialization/deserialization you can give http://laravel.com/ a try. It is build on symfony and you can generate an api on the fly.