Laravel 5 XML Parsing - google-maps

I have created a storelocator.php in my view with the following code :
<?php
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
header("Content-type: text/xml");
foreach ($results as $value) { // ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$value->dealer_name);
$newnode->setAttribute("address", $value->address);
$newnode->setAttribute("lat", $value->lat);
$newnode->setAttribute("lng", $value->lng);
$newnode->setAttribute("type", $value->outlet_type);
}
echo $dom->saveXML();
?>
My Route is:
Route::get('/storelocator','SearchController#showReviews');
my ShowReviews Function
public function showReviews() {
//$dealer_address = dealer_addresss::lists('dealer_name','address','lat','lng','outlet_type');
//return view('/reviews',compact('dealer_address'));
$results = DB::table('dealer_address')->get();
//return view('/reviewresults',compact('results'));
return view('/storelocator',compact('results'));
}
All I get is a blank page:
When I click on source I am able to the xml content with the data but on hover it says
Attempt to use an XML processing instruction in HTML.
What am I doing wrong here ? Any guide would be immensely appreciated.

I have used this:
https://github.com/SoapBox/laravel-formatter
to make proper XML.
You probably have an error in your xml file.
Also, sending header in view is already too late.
Send it in your controller.
Related: How to make Laravel return a View's "Content-Type" header as "application/javascript"?

Related

Using Mojo::UserAgent and accessing the JSON in response?

How can I get access to JSON in a mojo response?
$txn = $ua->post( $url, $headers, json => {json} )
What's the way to get the JSON response from the txn?
I have several examples in my book Mojolicious Web Clients, but here's the deal.
When you make a request, you get back a transaction object:
my $ua = Mojo::UserAgent->new;
my $tx = $ua->post( ... );
The transaction object has both the request and response (a major feature that distinguishes Mojo from LWP and even other user agent libraries in other languages). To get the response, you can use the res or result methods. The result dies for you if it couldn't make the request because a connection error occurred (ENONETWORK):
my $res = $tx->result;
Once you have the response, there are various things you can do (and these are in the SYNOPIS section of the Mojo::UserAgent. If you want to save the result to a file, that's easy:
$res->save_to( 'some.json' );
You can turn the content into a DOM and extract parts of HTML or XML:
my #links = $res->dom->find( 'a' )->map( attr => 'href' )->each;
For a JSON response, you can extract the contents into a Perl data structure:
my $data_structure = $res->json;
However, if you wanted the raw JSON (the raw, undecoded content body), that's the message body of the request. Think of that like the literal, unfiltered text:
use Mojo::JSON qw(decode_json);
my $raw = $res->body;
my $data_strcuture = decode_json( $raw );
Since this is the response object, Mojo::Message and Mojo::Message::Response show you what you can do.
Here's a complete test program:
#!perl
use v5.12;
use warnings;
use utf8;
use Mojo::JSON qw(decode_json);
use Mojo::UserAgent;
use Mojo::Util qw(dumper);
my $ua = Mojo::UserAgent->new;
my $tx = $ua->get(
'http://httpbin.org/get',
form => {
name => 'My résumé'
},
);
die "Unsuccessful request"
unless eval { $tx->result->is_success };
my $data_structure = $tx->res->json;
say dumper( $data_structure );
my $raw = $tx->res->body;
say $raw;
my $decoded = decode_json( $raw );
say dumper( $decoded );
I was able to get access to this data like this,
my $api_order = $tx_cart->result->json->{data};
It's in result not in body.

Get Facebook page posts to Wordpress with JS SDK or PHP SDK

I want to get the posts from a Facebook page and show them on a Wordpress site.
Firstly, I tried with the JavaScript SDK and succeeded, but couldn't figure out how to call the access token from server-side.
Secondly, I've been trying with the PHP SDK, but can not get it to work no matter what I try.
I've been over the documentation so many times I've lost count. It just doesn't make sense to me.
This is the PHP where I'm currently at:
require_once( 'out-fb/src/Facebook/autoload.php' );
$app_id = "{app-id}";
$secret = "{app-secret}";
$access_token = "{access-token}";
$fb = new Facebook([
'app_id' => $app_id,
'app_secret' => $secret,
'default_graph_version' => 'v5.0',
'default_access_token' => $access_token
]);
try {
// $response = $fb->getClient()->sendRequest($request);
$response = $fb->get('/{page-id}}/posts?fields=message,full_picture,created_time&limit=5');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
I don't exactly know how to translate the JSON and get the code to my front-end, but even now I get an error from WP saying "The site is experiencing technical difficulties."
If someone could guide me through the PHP SDK or maybe show me how to place the access_token server-side with the JS SDK, I'd be so damn grateful!
Thanks in advance.
So I figured it out.
I still don't know exactly how I did it, but you can see here what eventually worked for me.
I think what broke it was using $fb = new Facebook([ instead of $fb = new Facebook\Facebook([
<?php
require_once 'out-fb/src/Facebook/autoload.php';
$app_id = "{app-id}";
$app_secret = "{app-secret}";
$access_token = "{access_token}";
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v5.0',
]);
$response = $fb->get('/{page-id}/posts?fields=message,full_picture,created_time&limit=5', $access_token);
$node = $response->getGraphEdge();
echo $node->getField('3')[message];

How to retrive the parameters in URL + laravel

for web services , how to retrieve the data json format using Laravel, can anybody help
https://test.com/admin/users/apiLogin?Login={"email":test#test.com,"pwd":testing}
in Controller
public function apiLogin(Request $request)
{
//$data = $request->json()->all();
$data=json_decode($request['Login']);
echo $data['email'];
echo "<pre>";
print_r($data);
echo "</pre>";
echo "testsetet";
exit;
}
To get the URL parameter use this:
$login = $request->input('login'); // or $request['Login']
The goal is to turn login (which is a string) into appropriate JSON format so that you can use json_decode.
I'll break it down in steps:
Remove all \n characters:
$login = str_replace('\n', '', $login);
Remove last comma
$login = rtrim($login, ',');
Add brackets
$login = "[" . trim($login) . "]";
Turn it into a PHP array:
$json = json_decode($login, true);
The issue here is that your url parameter 'Login' isn't properly formatted json. I would revisit your front-end code persisting this data and ensure it is formatting properly. True json should look like:
{"email":"test#test.com","pwd":"testing"}
As a BIG aside, never persist passwords in the URL. It is dangerously foolish. Use a POST request to do this. On the plus side, the back-end code will be virtually the same with your json_decode() logic.

Json call in joomla 2.5 component

I'm making a joomla 2.5 component and i trying to set in model or controller (what's the most appropriate ?) a json response of my DB request (for later get the json with angularJS).
Here's my model (with DB response):
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modelList' );
class MediastoreModelList extends JModelList
{
function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, type, designation', 'marque', 'prix');
$query->from('produits');
return $query;
}
}
My empty controller:
<?PHP
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class MediastoreController extends JController
{
}
My view
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view' );
class MediastoreViewList extends JView
{
function display($tpl = null)
{
$this->items = $this->get('items');
parent::display($tpl);
}
}
and my template
<?php
defined('_JEXEC') or die('Restricted access');
JHTML::script('media/com_mediastore/js/angular.min.js');
JHTML::script('media/com_mediastore/js/app.js');
?>
<?php
echo $this->items;
?>
<div class="content">
<p>Nothing</p>
</div>
How can i do that ?
Thanks a lot,
Antoine
Bit late .....
Hi I am working on something similar.
In your views (sitepart) add an other file called view.raw.php.
You can browse to this file by appending 'format=raw' at the end of the url.
For example: if your component view page in the browser is 'http://test.com/test( IF SEF is on ) then after appending th url shoul be like this
http://test.com/test?format=raw and in case SEF is not on the use & instead of ?
You can use this URL in the angularjs for http.get service.
In view.raw.php file make sure you just echo the results rather than parent::display($tpl);.
This did work for me.
PS: I am using angualrjs too but want to use the component with many menu items and the url keeps changing so am stuck on that part.
Hope this solves your issue.
Regards,
Jai

HTML response when passing an Object in Zend Controller using end_Json_Encoder::encode

I Have two problem in this Case :
I want to pass a JSON object that i created in Zend Controller
public function exampleAction() {
$answers = array();
for($i = 0 ; $i < 3 ; $i++)
{
$answer = new Answer();
$answer->answer_id = 5 ; // for example
$answer->thanked = 'true';// for example
$answers[] = $answer;
}
echo Zend_Json_Encoder::encode($answers);
}
the Jquery Post function is :
$.post(
"/memberactions/getthanks/",
{values:values},
function(res){
alert(123);
}
, 'json')
First Question :
why the return response is HTML ? the response must be in JSON ?
Second Question
the HTML response is like this
[{"__className":"Answer","thanked":"true","answer_id":"5"}]
How can I make the response like this :
- answer
thanked : true
answer_id : 5
as a JSON Object without the __className:"Answer" (does it hurt to have the class name in the response) ?
Have you disabled layout, viewRenderer etc.? Also, you should send appropriate headers. You can do all this at once using the JSON action helper:
$this->_helper->json($answers);
You could provide a toArray() method in Answer, which would return an array of relevant properties and then use it in your action:
$answers[] = $answer->toArray();