i am trying to apply order by query on my publication table and then applying json encode, its works fine before json encode. and even after json encode in window, but on live server json encoding is not working, can anyone plz guide me where i am doing mistake.
the code is here below
public function articles($offset = null) {
$order=array('Publication.id' =>'DESC');
$total2 = $this->Publication->find('all', array('limit' => 10, 'offset' => $offset * 10, 'order' => $order));
echo json_encode($total2); die();
}
Related
I am trying to return the JSON data from a third party API with Laravel-Guzzle. Everything is working but the value is different it's like some kind missing characters specially on images.
Here's Laravel-Guzzle code :
$client = new \GuzzleHttp\Client();
$request = $client->request('GET',
'https://targetweb.com/api/v2/search_items/?', [
'query' => [
'by' => 'relevancy',
'keyword' => 'productx',
'limit' => 50,
'newest' => 0,
'order' => 'desc',
'page_type' => 'search',
'version' => 2
]
]);
$array = json_decode($request->getBody()->getContents(), true);
$nArrItems = count($array['items']);
for ($x=0;$x<$nArrItems;$x++){
echo $array['items'][$x]['name'].'<br/>';
$nImages = count($array['items'][$x]['images']);
for($z=0;$z<$nImages;$z++){
echo $array['items'][$x]['images'][$z]."<br/>";
}
}
Result on my browser running Laravel
It has same result as with API tester
Result API tester
but if I compare it(the above) with chrome inspect
Chrome inspect
in my browser : "309adc074bfeef9ad6d13561ef2","70eb....
in API tester : "images":["309adc074bfeef9ad6d13561ef2","70eb....
in Chrome inspect : "images":["309adc074bfeef9ad6d13561ef2e3ad1","70eb....
It has missing characters that does not show in my browser & API Tester.
Any help as why this is happening would be greatly appreciated.
I'm working on a Laravel project that implements react-jsonschema-form and I need to convert the values saved in the database to an associative array so I can pluck certain values from it. However I am getting strange results when doing so.
Here is the code I use to grab the JSON data from the table and then convert to an array:
$form = Form::where('id', $formId)->get();
$converted = json_decode($form[0]->form_data, true);
$formArray = print_r($converted, 1);
return $formArray;
For testing purposes I am simply rendering the data in the browser.
The result from the above return is:
Array
(
[1] => Array
(
[1.1] => New
[1.2] => Ms
[1.3] => Isobel Fleming
[1.4] => Array
(
[uprn] => 52375918
[address_1] => Fake Street
[address_2] =>
[address_3] =>
[town_city] => BRISTOL
[postcode] => BS1 3KE
)
[1.5] => 0129711011
[1.6] => 0800999111
[1.7] => 0781100022
[1.8] => isobelfleming#jourrapide.com
)
)
which is great. However when I try and access anything from it like:
return $formData[1][1.1]
I get:
String offset cast occurred
If I try using a string:
return $formData[1]['1.1']
I get:
Illegal string offset '1.1'
So I am not sure what to do to access this data. The problem is, although it's not ideal to have the associate keys with decimals in them, this is the way the schema is set up and it's several thousand lines long - this is just a snippet of the form data.
Is there anything that can be done in order to get the data from this array?
I figured it out. It seems like the following line was the problem:
print_r($converted, 1);
It didn't need to be printed as an array, I should have just used the $converted variable to access the data like so:
return $converted[1]['1.1'];
I am trying to create efficient JSON Response controllers for AJAX. So far, instead of passing whole entity to JsonResponse I am creating arrays with necessary data inside where I can easily manage output data leaving less work for JavaScript. My action looks something like this:
public function getOffersAction(Request $request)
{
if (!$request->isXmlHttpRequest()) {
return new JsonResponse(array('message' => 'You can access this only using Ajax!'), 400);
}
/** #var OfferRepository $offerRepository */
$offerRepository = $this->getDoctrine()->getRepository('IndexBundle:Offer');
$offers = $offerRepository->findBy(array('state' => 'available'));
$offersArray = array();
/** #var Offer $offer */
foreach ($offers as $offer) {
$areasArray = array();
foreach ($offer->getAreas() as $area) {
$areasArray[] = array(
'name' => $area->getName()
);
}
$offersArray[] = array(
'id' => $offer->getId(),
'code' => $offer->getCode(),
'title' => $offer->getTitle(),
'city' => $offer->getCity(),
'country' => $offer->getCountry()->getName(),
'latitude' => $offer->getLatitude(),
'longitude' => $offer->getLongitude(),
'areas' => $areasArray
);
}
return new JsonResponse($offersArray, 200);
}
It is all good, ajax is working fast.
At this point I started googling searching if this is a right approach to it. I found out about JMSSerializerBundle which serializes entities. I tried using it, but I am facing problems serializing relationships and how to access related entities data using JS. It is so complicated leaving so many proccessing to do for JS that I start doubting that it is a good approach.
What do you think? What is your experience with it? Which approach is better and why?
I prefer the symfony normalizer/serializer approach.
http://symfony.com/doc/current/components/serializer.html
As described, you can overide serializer to serialize your object in the same custom way for your whole application
Within an extension (JSON access), I'm calling getContent() with:
$items = $this->app['storage']->getContent($contenttype, $options);
$response = $this->app->json($items);
return $response;
The options array is:
[
'limit' => 5,
'page' => 3
]
But getContent only returns the latest 5 results, not calculating the offset from the page variable. Is there another setting that I have to change to get it to recognise the paging?
Calling getContent you need to specify that you want paging turned on, here's some adding 'paging'=>true to the options should work correctly.
I'm finding list of Areas for my CakePHP 2.x website and it supports, JSON output as below with find all method:
$this->Area->find('all', array('fields' => array('id', 'name', 'order'), 'conditions' => array('Area.status' => 1)));
Below is my JSON output:
[{"Area":{"id":"2","name":"Area 1","order":"1"}},{"Area":{"id":"3","name":"Area 2","order":"1"}}]
Now Is that possible for me to remove Area tag which is repeating everytime?
Any patch for same? Do let me know if any suggestions / ideas.
on your view for output json write this:
echo json_encode(Set::extract('/Area/.', $area));
For me works fine.
CakePHP Provides some in-built library functions for data extraction from result set and output same as JSON format.
// initialize your function to render false and response type json
$this->autoRender = false; // no view to render
$this->response->type('json');
// Remove Area from array and encode
$area = Set::extract('/Area/.', $area);
$json = json_encode( $area);
$this->response->body($json);
Hope it helps !