json_decode differences between two physical PCs - php

Last few weeks I have been working from home for obvious reasons. We run our development environment via a stack of docker microservices for which we have a standard set of docker images and docker-compose files.
At work my stack works 100% without issue with regard to the calls for json_decode but when I am home and use the same files whenever json_decode is called the process "hangs". Putting some logging after the call is never reached.
One example of the call is
$ret[$row['an']]['sc'] = json_decode($row['channel']);
When the code reaches this it never gets out of it, no errors logged either.
To fix this I have to change the call to the following (please note this is running on PHP version 5.6.40)
$ret[$row['an']]['sc'] = json_decode($row['channel'],false,512,JSON_INVALID_UTF8_IGNORE);
My home dev machine is a Windows PC (Win 10) but I also have a Linux drive which I can boot into but this experiences the same problem.
Additional info:
This is working upon an array of json strings such as
{"phones":["44123456789","44123456788","44123456787"]}
{"phones":[]}
It hangs when the value been worked on is {"phones":[]} but if the json value has numbers it runs fine.
The json_decode is in fact hanging everything except when the value is NULL. If the value been decoded is null is carries on without issue.
On my work machine where everything runs without issue the data is the same oddly.
Additional info to support comments:
A var_dump of the first item that json is choking on
string(56) "{"phones":["44123456789","447838588850","447838588850"]}"
Output of the byte string for the failing item as per Gavins recommendation:
(
[1] => 7b2270686f6e6573223a5b223434313233343536373839222c22343437383338353838383530222c22343437383338353838383530225d7d
)
I also have tried the three methods outlined in the solution at How to detect malformed utf-8 string in PHP?'%2C%20%24string)%3B and all are returning true (true = valid)
UPDATED 19/06/2020 below
Testing the simple php code below even this fails on the json_decode. Json_encode works fine
<?php
$job[123] = [
'id' => 123,
'position' => [
'title' => 'PHP Developer',
],
];
$json = json_encode($job[123]);
echo($json);
$jsonStr = json_decode($json);
print_r($jsonStr);
```

Related

Unable to get Browsershot to work with query string parameters

I am running PHP Laravel 5.5.* with spatie/browsershot 3.24 and I am having an issue with pulling the HTML from a url that I generate from a route.
Code:
$url = route('pdfs.plans.results', [
'lang' => $culture->code,
'id' => $id,
'user_id' => \Auth::user()->id,
'pdf-token' => $token->token,
]);
And for now I'm just wanting to dd the raw html generated by this route:
dd(Browsershot::url($url)->bodyHtml());
However, this is failing and it appears to be due to the query parameters in the url string Example url: http://localhost/pdf/plans/results/share?lang=en_US&id=17&user_id=8&pdf-token=PEvWJ7MB7sgQWM9AUfciceL4zNvZrLaSeemGf5J4kGzfOKvaMe8L0FTf32Da
As soon as it hits the second parameter '&id=17' it breaks stating:
'id' is not recognized as an internal or external command
which tells me that NodeJS is what's breaking, I'm guessing the ampersand is throwing it off and it's thinking that I'm passing another command to the console, but I don't know how else to get around this.
Is there a better way to pass the URL, or perhaps an alternative/better way to extract the rendered HTML from a Laravel blade (running VueJS) that I can then use for PDF generation or other business needs?
Thank you!
Maybe windows only related issue as I havent tried it on Linux yet, but I escaped the & and all now works fine.
$url = str_replace('&', '^&', $url)

parse_url giving host name as stars when debugged (but can still get value)

Using PHP 5.5.12
Using CakePHP 2.6.7
Running
debug($url); // output = "http://google.co.uk"
debug(parse_url($url));
/* output = array(
'host' => '*****',
'scheme' => 'http'
)
*/
I had been using this without trouble but now copy/pasting a section of my code to have it as method (to save repeating myself) has started giving me this output. But testing it back in the same place I had it originally gives me this output too.
Can anyone explain why the hostname is stars and why the rest of the array doesn't appear (I realise all other elements should be expected to be NULL)?
Edit
Just tried it again with a url that had a path to a page after the host. The path shows up fine but the host is still starred out.
Partial Answer
Just thought to try debug(parse_url($url)['host']) and it prints the host correctly. I realised that the other elements would only be set if they exist in the url.
However, can anyone explain why printing out the array prints several stars instead of the hostname even though it is definitely stored there?
The reason this happens is because of how debug() works. Many moons ago people were not pleased that they could accidentally have their database credentials dumped out in error pages (which use the same underlying code as debug()). Because of this, debug() and Debugger::export() blacklist a set of array keys that could have database credentials. The following keys are replaced with ***'s:
password
login
host
database
port
prefix
schema

CakePHP find method crashes on live server when more than a few entries

I'm encountering a strange error in CakePHP and I have no idea how to figure out what is going on.
I am using a Ajax request to load dynamic data for a dataTable and it works perfectly on my localhost. For some reason I can't get it to work on my live server when I have more than a few entries.
After debugging I found that it's the find function that is not working. When I have more than a few entries in my response array than one of the arrays('SollProject') is just simply completely empty and the whole script stops working.
Here is my Code:
$project = $this->Project->find("first", array('conditions'=>array('id'=>$project_id) ,
'contain' => array(
'SollProject' => array(
'conditions'=> array(
'SollProject.date >=' => $year.'-01-01',
'SollProject.date <=' => $year.'-12-31'
)
),
'HabenProject'=> array(
'conditions'=> array(
'HabenProject.date >=' => $year.'-01-01',
'HabenProject.date <=' => $year.'-12-31'
)
))));
Any idea what I am doing wrong here or why it is not working on the live server? There is no afterFind method in case you were wondering.
Maybe I should mention that I tried error debugging already and I don't get any error messages. I also tried the same sql code on my sql server and it works perfectly. There is also no memory issues since the memory I'm using is way beyound what I'm usually using and it's only a json response.
The function itself also works for small entries (not more than maybe 5 entries) but as soon as I have a little bit more entries in the database it just crashes.
The interesting thing is that I can debug($project['HabenProject']) and it shows me all HabenProject but if I try debug($project['SollProject']) or just debug($project) the variable is just empty. It's not even declared as array. There is just no output. The rest of the page is still showing.
Basic debugging steps:
Set debug to 2
Check your CakePHP apps logs
Check the php logs
Check the webserver logs
However, I guess it's a memory limit and debug is off, that's probably why you get a white page. Check the memory usage and increase the php memory limit.
Ok, after lots of debugging it looks like I found the problem:
For some reason every entry with an Umlaut (äüö) in the database produced an error that caused the result array of the find function to not work anymore.
Turns out the problem was the live server database utf-8 encryption was commented off...

Laravel's Response::json() returns nothing to the client under HHVM

I have HHVM running on a virtualbox VM, with the webroot mapping to my local laravel install, being served out at an internal IP.
Locally, I'm serving the site out under http://[localhost]:8000.
The codebase is identical.
code of MembersController.php (resourceful controller):
public function show($id)
{
$member = Member::findOrFail($id);
$data = array();
$data['id'] = $member->id;
$data['first_name'] = $member->first_name;
$data['last_name'] = $member->last_name;
return Response::json($data);
}
Assuming everything is working normally:
When I run a GET request to LOCALHOST: http://[localhost]:8000/api/v1/member/1, the client returns the JSON as normal - all good.
When I run a GET request to HHVM (same client, identical codebase): http://[vm_ip_address]/api/v1/member/1, the client receives no data.
The data is being passed back through the calls within HHVM though, as if I change the 'return' to 'echo', the payload is returned in both cases (headers also)
It looks like HHVM is affecting with laravel's Response::json() function and disallowing the reply contents from being displayed in the client.
Has anyone else seen this?
This is not something I can set up a unit test for, as it always passes, because the final reply always has data in it :/
Any input would be great - I'm interested to learn how to get around this.
Thanks.
Sadly you're probably going to have to get your hands dirty debugging. HHVM probably has a very slight difference in how it does something which this code-path is sensitive to. We pass 100% of laravel unit tests, but there probably isn't one covering this case.
If you can, please trace down the code to where the data changes. Put in echos and error_logs until you can build a very small test case then then open an issue on github and we'll get it fixed.

Variable not holding value and dumping contents

Normally I can find what I am looking for on here, even if I have to piece a few things together. This is the first time I haven't been able to find anything!
Ok so I have an API that I wrote and everything was working fine on my localhost.
When we transferred it to the server, that's when things started going wrong. That is why I have tagged this with Debian and Apache as I feel the issue could lie there somewhere.
The API is working correctly and is returning the JSON string as it should.
The result of the API call gets loaded into a variable (on WAMP on my PC it works fine) and I then make use of the variable later in my code.
On the server the variable gets dumped and then loses its value.
<?php
include 'api.php';
$departure = $_GET['departure'];
$arrival = $_GET['arrival'];
$routes = call("http://example.com/bus/$departure/$arrival");
echo '<br><br>'; //These two lines are here only for my debugging
var_dump($routes); //These are not the issue
?>
The var_dump at the end you would think should dump everything a second time but it returns string(0) ""
That is what I mean by it not holding its value
Any ideas?
SUMMARY: The bit that gets dumped the the top of the page is exactly what should be returning. The main issue is that the output from the call function gets outputted onto the page as though a var_dump is there. The $routes variable isn't storing it hence the actual var_dump returning an empty string

Categories