Does anyone know why I get a parsing error with the following code?
'always_load' => array(
array('auth');
array(
array('auth' => PKGPATH.'auth/')
);
'packages' => array(
'orm',
),
A config file is one long nested or multi-dimensional array, so you never use semi-colon's, only comma's. This is standard PHP array notation.
Related
I have the following perl code in where I have a perl structure as follows:
`
use Data::Dumper;
my %data = (
'status' => 200,
'message' => '',
'response' => {
'name' => 'John Smith',
'id' => '1abc579',
'ibge' => '3304557',
'uf' => 'XY',
'status' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' )
}
);
my $resp = $data{'status'};
print "Response is $resp \n";
print Dumper(%data->{'response'});
Getting the status field works, however If I try something like this:
my $resp = $data{'response'}
I get Response is HASH(0x8b6640)
So I'm wondering if there's a way I can extract all the data of the 'response' field on the same way I can do it for 'status' without getting that HASH...
I've tried all sort of combinations when accessing the data, however I'm still getting the HASH back when I try to get the content of 'response'
$data{'response'} is the correct way to access that field on a hash called %data. It's returning a hash reference, which prints out by default in the (relatively unhelpful) HASH(0x8b6640) syntax you've seen. But if you pass that reference to Dumper, it'll show you everything.
print Dumper($data{'response'});
to actually access those subfields, you need to dereference, which is done with an indirection -> operation.
print $data{'response'}->{'name'}
The first access doesn't need the -> because you're accessing a field on a hash variable (i.e. a variable with the % sigil). The second one does because you're dereferencing a reference, which, at least in spirit, has the $ sigil like other scalars.
Thanks for your posts. I fixed the code as follows:
use Data::Dumper;
my %data = (
'status' => 200,
'message' => '',
'response' => {
'name' => 'John Smith',
'id' => '1abc579',
'ibge' => '3304557',
'uf' => 'XY',
'status' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' )
}
);
my $resp = $data{'response'};
print Dumper($resp);
Now it works like a charm, and I'm able to get the data I want.
I am using Perl LWP::UserAgent to get response from an API. Everything works good except one issue.
The API that i am using it returns response in JSON format. But I am getting it as string when i get the response through LWP module, Something like below.
$VAR1 = '
{"status":"success","data":[{"empid":"345232","customername":"Lee gates","dynamicid":"2342342332sd32423"},{"empid":"36.VLXP.013727..CBCL..","customername":"Lee subdirectories","dynamicid":"223f3423dsf23423423"}],"message":""}'
I did "print Dumper $response" to get the output.
One more thing, The challenge is that my client do not want to go with Perl module for JSON (use JSON::Parse 'parse_json';).
Any help would be appreciated!
You need to decode the JSON string into a Perl data structure. If your version of perl is 5.14+, JSON::PP is included in core, so nothing to install.
use warnings;
use strict;
use Data::Dumper;
use JSON::PP qw(decode_json);
my $json = '{"status":"success","data":[{"empid":"345232","customername":"Lee gates","dynamicid":"2342342332sd32423"},{"empid":"36.VLXP.013727..CBCL..","customername":"Lee subdirectories","dynamicid":"223f3423dsf23423423"}],"message":""}';
my $perl = decode_json $json;
print Dumper $perl;
Output:
$VAR1 = {
'status' => 'success',
'message' => '',
'data' => [
{
'dynamicid' => '2342342332sd32423',
'customername' => 'Lee gates',
'empid' => '345232'
},
{
'empid' => '36.VLXP.013727..CBCL..',
'customername' => 'Lee subdirectories',
'dynamicid' => '223f3423dsf23423423'
}
]
};
I have the following cURL statement
curl http://localhost/ocpu/library/stats/R/t.test -d "x=x0e48e4cb3f&y=x09aaf63ea6"
When I run this in the terminal everything is fine and I get the response I want, a completed calculation.
Now I'm trying to build this into a PHP application with the use of Guzzle. I have the following code.
$result = $this->client->request('POST', 'http://localhost/ocpu/library/stats/R/t.test', ['json' => ["x" => $x, "y" => $y],
'header' => ["content" => "application/x-www-form-urlencoded"]])->getBody();
$x and $y contain strings with the values of x and y in the cURL statement.
This gives me the error "400 Bad Request response:
not enough 'x' observations"
Using the OpenCPU API Explorer I've figured out that I get the same error when trying to add the x and y parameters as primitive strings by adding quotes around them.
So my problem seems to be that Guzzle sends the x and y parameters as strings instead of Temp keys.
How can I get it to send the exact cURL paramater?
Thank you all in advance.
$params = [
'x' => 'value',
'y' => 'value',
];
$response = $client->post($uri, [
'form_params' => $params,
]);
I believe the issue is within the code sample that you have supplied. You are using 'json' when 'form_params' is (what i interpret) you are looking for. When 'form_params' is used, content-type headers are automatically set for form data.
More information can be found within Guzzle 6 Request Options
in ZF2 skeleton, router configuration uses a key :
'__NAMESPACE__'
precisely :
'__NAMESPACE__' => 'Application\Controller',
cf:
https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php#l32
We tried in our modules router config to use without quote:
__NAMESPACE__ => 'Application\Controller',
but it seems to break configuration.
why do we use quote instead of
__NAMESPACE__
to get its value ?
because by default, config files hasn't namespace declared. Config parser can read string
'__NAMESPACE__'
and determine correctly the namespace.
If you want to use it without quotes, you can declare in your config file :
namespace Application;
and use __NAMESPACE__ without quote.
That's why you can see sometimes in tutorials for Doctrine config's sample like :
'doctrine' => array(
'driver' => array(
'application_entity' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => __DIR__ . '/../src/' . __NAMESPACE__ . '/Entity',
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => 'application_entity',
)
)
)
),
I have a CakePHP app that is being moved to Sql Server from MySql. There is one query that does not seem to transfer correctly:
$this->Model->find('all', array(
'conditions' => array(
'Model.column' => array(1, 2, 3)
)
)
);
When I use this syntax with mysql, it seems to 'unpack' the array
correctly, and the query generated is something like
"...WHERE 'Model.column' IN (1, 2, 3)..."
When I use sql server, the query generated is
"...WHERE 'Model.column' IN 'Array'"...
which obviously generates an error. I posted this question on the CakePHP Google Group yesterday, but have not received a reponse, so I thought I would try SO. If anyone has any ideas/suggestions I would appreciate it.
The code that generates this is in dbo_source.php (function conditionKeysToString) and while specific database drivers can override this, I have never seen that done.
I have 1.2.5, 1.2.6 and 1.3.0-RC1 on my system and they all append ' IN (' and nothing overides that function. There is no instance of just appending ' IN ' and then deciding if it is an array or scalar value. The word Array is what happens when an array variable is evaluated in a string context. EG: php -r '$a = array(1,2,3); echo $a;' will output Array.
Check the cake/VERSION.txt file in both. If they are different, back up the cake directory on the SQL Server instance and replace it with the one from MySQL.
If they are the same try going to the cake/libs/model/datasources directory. Hopefully you have Unix or Linux because "grep -r ' IN ' *" will help you out immensely. Otherwise compare dbo_soures.php and dbo/dbo_mssql.php between both installs of cake and see if there are any differences in conditionKeysToString.
Ultimately I would upgrade to 1.2.6 just so that you get any and all fixes.
Try passing that condition as a string:
$this->Model->find('all', array(
'conditions' => array(
'Model.column in (1, 2, 3)'
)
)
);
Try it like this:
$this->Model->find('all', array(
'conditions' => array(
'Model.column' => array(
'OR' => array(1, 2, 3)
)
)
)
);