Laravel JSON maxCDN / Chrome JSON Formatter does not work - php

Currently i am working with Laravel and the maxcdn API.
This is my very little test code
Route::get('cdn', function() {
$api = new MaxCDN("myurl","mykey","mysecret");
$data = $api->get('/account.json');
return response()->json($data);
});
The header is set to json as you see
application/json
Now if i visit my /cdn url the Chrome JSON Formatter should be of course formate the output. But it does not work, i only get this output
"{\"code\":200,\"data\":{\"account\":{\"id\":\"12312\",\"name\":\"My Name \",\"alias\":\"myurl\",\"date_created\":\"2015-05-17 07:51:50\",\"date_updated\":\"0000-00-00 ........
I am working on my homestead machine
php -v
PHP 5.6.6-1+deb.sury.org~utopic+1 (cli) (built: Feb 20 2015 11:25:37)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

The $api->get('/account.json'); call returns a string that contains the json data. You are then passing this string to the response()->json(); method, and that is the output you are seeing.
You need to run the string through json_decode() in order to convert the json string into a PHP object. Once you do that, you can pass that result to the response()->json(); method, and you will get the result you're looking for.
Route::get('cdn', function() {
$api = new MaxCDN("myurl","mykey","mysecret");
$stringData = $api->get('/account.json');
// convert the json string into an actual PHP object
$data = json_decode($stringData);
// respond with the object
return response()->json($data);
});

Related

Invalid Encoding json (problem when displaying Cyrillic)

I am trying to deduce a file from the form, I have a problem when displaying Cyrillic
Php (7.1)
<?php
header("Content-type: application/json; charset=utf-8");
file_put_contents('export.json', json_encode($_REQUEST) );
?>
Output
You_name field is written in characters.
{"Entry_ID":"71","You_name":"\u041a\u043e\u0437\u043b\u0430\u043d\u0431\u0435\u043a \u0410\u043c\u0438\u0440 \u041f\u043e\u043c\u0438\u0434\u043e\u0440\u043e\u0432\u0438\u0447","You_phone":"7 (902) 998 1019","You_date":"19\/06\/2019"}
6 year old answer still stands: Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?
json_encode( $text, JSON_UNESCAPED_UNICODE );
Example:
/tmp $ cat test.php
<?php
echo json_encode(['key'=>'ыфва']), PHP_EOL;
echo json_encode(['key'=>'ыфва'], JSON_UNESCAPED_UNICODE), PHP_EOL;
/tmp $ php test.php
{"key":"\u044b\u0444\u0432\u0430"}
{"key":"ыфва"}
/tmp $ php --version
PHP 7.3.6 (cli) (built: May 31 2019 23:38:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
with Zend OPcache v7.3.6, Copyright (c) 1999-2018, by Zend Technologies
You should use $_POST to get the form data like as below
header("Content-type: application/json; charset=utf-8");
file_put_contents('export.json', json_encode($_POST) );
Try this (source) :
header("Content-type: application/json; charset=utf-8")
$convert = file_put_contents('export.json', json_encode($_REQUEST));
$convert = iconv('CP1251', 'UTF-8', $convert);

PhP shell execute ignores Exceptions

I have the strangest bug. I was like wtf^12.
I have a script wchich calls following function:
function checkNewFilename($newFilename)
{
if (file_exists($newFilename)) {
if (!unlink($newFilename)) {
debugDeploy($newFilename . ' already exists and could not be deleted.');
throw new Exception($newFilename . ' already exists and could not be deleted.');
}
}
}
It gets used like this:
$newFilename = $defaultConfig . DIRECTORY_SEPARATOR . 'client.xml';
checkNewFilename($newFilename);
echo 'wtf is happening here';
copy($configFilename, $newFilename);
Now when i run this code with normall browser/xdebug i get a cachable fatal error as it should be. BUT when i run the script via shell on
php script.php param param param
Somthing nuts happens. IT ignores the exceptions and gous as nothing happend. I get the error before the exception AND the echo after the function call... wth.
I am runing on
PHP 5.3.3-7+squeeze14 with Suhosin-Patch (cli) (built: Aug 6 2012
14:18:06) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0,
Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
Anyone has any idea what causes this strange behavior?

Laravel Route: Controller method not found

I took a copy of my live Laravel 4 app and set it up on an AWS Ubuntu instance.
Here's the same route that works on live site, http://pixcel.com/archivecloud/786531 ,
and the same route with same code (I think) on the EC2 instance
54.186.47.148/archivecloud/786531
Error returned is: Controller method not found
Live site:
PHP 5.3.27 (cli) (built: Feb 21 2014 07:53:29)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
with the ionCube PHP Loader v4.2.2, Copyright (c) 2002-2012, by ionCube Ltd., and
with Zend Guard Loader v3.3, Copyright (c) 1998-2010, by Zend Technologies
Apache: not sure, it's shared hosting with site5
EC2:
PHP 5.4.25-1+sury.org~precise+2 (cli) (built: Feb 12 2014 10:45:30)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
Server version: Apache/2.2.22 (Ubuntu)
Relevant code:
Route LIVE:
Route::get('archivecloud/{id}', 'CloudHomeController#embed');
Route EC2: Route::get('archivecloud/{id}', 'CloudHomeController#embed');
Controller LIVE:
class CloudHomeController extends CloudBaseController {
public function embed($id){
$result = CloudFilm::getInstance()->getFilmByEmbedId($id);
$cliptype = "film";
}}
Controller EC2:
class CloudHomeController extends CloudBaseController {
public function embed($id){
$result = CloudFilm::getInstance()->getFilmByEmbedId($id);
$cliptype = "film";
}}
Looks like the Environment variables are empty. Will look this up.

Why do I get slaveOkay = 0; Timeout = 30000 MongoPHP

I am using Mac, netbeans, x-debug, with Mongo PHP. I am trying to run a basic search like:
$results = $mongo->$col->find();
However during debug, the value for $results is slaveOkay = 0 and timeout = 30000. Why is this so? Some version information about the tools installed are:
PHP 5.4.20 (cli) (built: Sep 24 2013 10:10:10) (DEBUG) Copyright (c)
1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013
Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
MongoDB shell version: 2.4.6
Thanks.
Where does that $col variable come from?
And where does $mongo come from?
The correct way to query a collection is
<?php
$mongo = new MongoClient(...);
$collection = $mongo->databaseName->collectionName;
$results = $collection->find();
?>
Then you can iterate over $results like so:
foreach($results as $document) {
/* Do something with $document */
}
See http://php.net/mongocollection.find and http://php.net/mongo.queries for more details

Segmentation Fault error in PHP, using SOAP to connect to SalesForce

I'm integrating my software (PHP) with SalesForce, using the SalesForce PHP Toolkit.
So far, everything's worked great, but when I started writing the code to call convertLead(), I got a "Segmentation Fault" error.
This is the code I'm running:
require_once('../salesforce/SforceEnterpriseClient.php');
ini_set('soap.wsdl_cache_enabled', 0);
$SForce = new SforceEnterpriseClient();
$result = $SForce->createConnection('../salesforce/enterprise.wsdl.xml');
$result = $SForce->login('user', 'pass+token');
echo "Logged In!";
$data = array(
'convertedStatus' => 'Converted',
'leadId' => '00QC000000mDcmJMAS'
);
$result = $SForce->convertLead(array($data));
That's it. And i'm getting a Segmentation Fault. I tried using StdClass instead of a keyed array, same thing. The convertLead method in the SF toolkit is really simple, it just calls the same method into a SoapClient instance...
NOTE: I'm running this script from the CLI, not through Apache.
UPDATE: Just tried running "strace" with my script. The last lines of it are:
close(4) = 0
write(1, "Logged IN!", 10Logged IN!) = 10
open("error_log", O_WRONLY|O_CREAT|O_APPEND, 0644) = 4
--- SIGSEGV (Segmentation fault) # 0 (0) ---
+++ killed by SIGSEGV +++
Also, in case it's relevant:
php --version
PHP 5.2.13 (cli) (built: Jul 17 2010 22:01:13)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
with the ionCube PHP Loader v3.3.20, Copyright (c) 2002-2010, by ionCube Ltd., and
with Zend Optimizer v3.3.9, Copyright (c) 1998-2009, by Zend Technologies
This also happens in my dev machine (Windows), so I doubt it's the Accelerators or anything like that:
php --version
PHP 5.2.13 (cli) (built: Feb 24 2010 14:37:44)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
This may or may not be specific to SalesForce. Probably not, seems like a bug in the SOAP PHP library. Maybe the request/response are broken, but I can't see those because they're HTTPS.
Any ideas how I can go about diagnosing (or more importantly, working around) this issue?
Thank!
Daniel
There is a bug (still after 4 years) in the PHP soap extension. It has to do with how it handles WSDL caching. Disabling caching with ini_set() does not work. You also need to turn off caching for your particular client instance.
return new SforceEnterpriseClient('../salesforce/enterprise.wsdl.xml', array(
'cache_wsdl' => WSDL_CACHE_NONE
));
This is true even when using the native PHP SoapClient class.
Ok, this doesn't solve the underlying issue, but, in case someone got this same problem with SalesForce and PHP...
Despite what the documentation implies, fields SendNotificationEmail and OverwriteLeadSource ARE MANDATORY, you MUST specify them in the call.
Not doing so should give you a nice error, not a SegFault, but still, that solves it.

Categories