Kohana3 Function name must be a string exception in Route class - php

I have the following code in Welcome_Controller action_index method:
$route = new Route('(<controller>(/<action>(/<id>)))');
$body = $route->uri(array(
'controller' => 'welcome',
'action' => 'index',
'id' => '10'
));
echo Debug::vars($body);
When $route->uri method is called, I am getting an error:
ErrorException [ Fatal Error ]: Function name must be a string in SYSPATH/classes/Kohana/Route.php [ 589 ]
584 }
585
586 return array($result, $required);
587 };
588
589 list($uri) = $compile($this->_uri, TRUE);
590
591 // Trim all extra slashes from the URI
592 $uri = preg_replace('#//+#', '/', rtrim($uri, '/'));
593
594 if ($this->is_external())
Why am I getting this error? According to Kohana install.php file, my environment passed all requirements.
$compile is an anonymous function, but according to PHP Manual there were introduced in PHP 5.3.0 version, which I have on my hosting environment. Maybe there is an option to enable/disable anonymous functions in PHP?
You can find whole code here: https://github.com/DamianKedzior/kohana/tree/3.3/route_uri_exception

Related

Creating a http Client in Cake 3

I am having trouble getting a response from an API using a http Client in Cake PHP 3
I want to send the following GET request but I cannot manage to return a result. If visit this url with a browser i get a response.
http://XX.XX.XX.XX:8020/mvapireq/?apientry=newuseru&authkey=XXXXXXXXXX&u_username=NEWUSERNAME&u_password=PASSWORD&u_name=NAME&u_email=EMAIL&u_phone=PHONE&u_currency=USD&u_country=USA&u_address=x&deviceid=DEVICEID&now=555
When I try to send the same request with http Client my reponse object is always null
use Cake\Network\Http\Client;
use Cake\Network\Http\Response;
public function foo(){
$http = new Client();
$data = [
'apientry' => 'newuseru',
'authkey' => 'XXXXXXXX',
'u_username' => 'NEWUSERNAME',
'u_password' => 'PASSWORD',
'u_name' => 'NAME',
'u_email' => 'EMAIL',
'u_phone' => 'PHONE',
'u_currency' => 'USD',
'u_country' => 'USA',
'u_address' => 'x',
'deviceid' => 'DEVICEID',
'now' => '555'
];
$response = $http->get('http://XX.XX.XX.XX:8020/mvapireq', $data);
debug($response->body);
debug($response->code);
debug($response->headers);
}
This is the results from the debug() it seems to me like the request is not being sent.
Notice (8): Trying to get property of non-object [APP/Controller/UsersController.php, line 1159]
/src/Controller/UsersController.php (line 1159)
null
Notice (8): Trying to get property of non-object [APP/Controller/UsersController.php, line 1160]
/src/Controller/UsersController.php (line 1160)
null
Notice (8): Trying to get property of non-object [APP/Controller/UsersController.php, line 1161]
/src/Controller/UsersController.php (line 1161)
null
/src/Controller/UsersController.php (line 1163)
null
I've tried many different ways of structuring $http and all of them have returned the same result. I really can't figure out whats going wrong. Any help would be great.
You can specify hostname and port in the constructor of the Client:
<?php
use Cake\Network\Http\Client;
// ....
public function foo(){
$http = new Client([
'hostname' => 'XX.XX.XX.XX',
'port' => '8020'
]);
$data = [
'apientry' => 'newuseru',
//....
'now' => '555'
];
$response = $http->get('/mvapireq', $data);
// ...
}
And check your system:
firewall (os)
allow_url_fopen (in your php runtime configuration)

Uploading to S3 with Laravel 5.2

Having an issue with the s3 driver for Laravel 5.2. The error i'm getting is this:
Found 1 error while validating the input provided for the HeadObject operation:
[Key] must be at least 1 characters long. Value provided is 0 characters long.
I'm using the league flysystem V3 as stated in Laravel docs. When I follow the stack track and start dumping out the vars the 'Key' value is always empty but i've set it all up in my config file.
Here are the top lines from my stack trace
in Validator.php line 38
at Validator->validate('HeadObject', object(StructureShape), array('Bucket' => 'monstervsl', 'Key' => '', '#http' => array())) in Middleware.php line 77
at Middleware::Aws\{closure}(object(Command), null) in S3Client.php line 710
at S3Client::Aws\S3\{closure}(object(Command), null) in S3Client.php line 729
at S3Client::Aws\S3\{closure}(object(Command), null) in Middleware.php line 53
at Middleware::Aws\{closure}(object(Command), null) in SSECMiddleware.php line 59
at SSECMiddleware->__invoke(object(Command)) in AwsClient.php line 208
As you can see it's getting the bucket from my config but not the key it's empty.
Here is my filesystem.php file
's3' => [
'driver' => 's3',
// 'key' => env('S3_KEY'),
// 'secret' => env('S3_SECRET'),
'key' => '8tfnxo8abgn7voaex8rgv', // <- Not my real key
'secret' => 'aw7btx49wXNF7AGWV', // <- not my real secret
'region' => 'eu-west-1',
'bucket' => 'monstervsl',
],
Here is my controller, it's fairly straight forward, I don't think the put contents stuff is relevant but added it anyway
// Write the contents to a new file on disk
$view = view('iframe')->with('json', $video->toJson());
$contents = $view->render();
$token = '12345';
$filePath = public_path() .'/iframes/' . $token . '.html';
file_put_contents($filePath, $contents);
Storage::disk('s3')->put('/', file_get_contents($filePath));
You have to give the destination file path/name where you want to save this file on S3 bucket as the first argument of put function. Currently you are trying to save the file at the root of bucket without any name. Try something like this:
Storage::disk('s3')->put('filename.html', file_get_contents($filePath));
This full path of the file is the Key in this context & that is what's missing in your original request.

S3Client PHP SDK: Object of class <class> could not be converted to string

I get the following errors when I try to use the AWS PHP SDK:
PHP Warning: Illegal string offset 'client.backoff' in C:\xampp\htdocs\aws_test_local\vendor\aws\aws-sdk-php\src\Aws\S3\S3Client.php on line 172
PHP Catchable fatal error: Object of class Guzzle\Plugin\Backoff\BackoffPlugin could not be converted to string in C:\xampp\htdocs\aws_test_local\vendor\aws\aws-sdk-php\src\Aws\S3\S3Client.php on line 172
PHP Warning: Illegal string offset 'signature' in C:\xampp\htdocs\aws_test_local\vendor\aws\aws-sdk-php\src\Aws\S3\S3Client.php on line 175
PHP Catchable fatal error: Object of class Aws\S3\S3Signature could not be converted to string in C:\xampp\htdocs\aws_test_local\vendor\aws\aws-sdk-php\src\Aws\S3\S3Client.php on line 175
They originate from the following code inside the S3Client.php file part of the AWS SDK.
public static function factory($config = array())
{
$exceptionParser = new S3ExceptionParser();
// Configure the custom exponential backoff plugin for retrying S3 specific errors
if (!isset($config[Options::BACKOFF])) {
$config[Options::BACKOFF] = static::createBackoffPlugin($exceptionParser);
}
$config[Options::SIGNATURE] = $signature = static::createSignature($config);
...
The Options-class is the Aws\Common\Enum\ClientOptions. If you look at it it defines a lot of constants like this:
const SIGNATURE = 'signature';
const BACKOFF = 'client.backoff';
I call the factory function in the following way:
$s3 = S3Client::factory(_PS_ROOT_DIR_.'/override/aws/aws-config.php');
My aws-config.php file looks like this:
<?php
return array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'key' => 'XXXXXXXXXXX',
'secret' => 'XXXXXXXXXXX',
'region' => 'eu-west-1'
)
)
)
);
?>
Any ideas? I installed the PHP SDK with Composer, so I'd expect any dependancies to be installed.
The argument to S3Client::factory() is supposed to be an array. You're giving it a filename that contains PHP code to return the array, but S3Client doesn't run the file. Try changing the file to:
<?php
$s3options = array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'key' => 'XXXXXXXXXXX',
'secret' => 'XXXXXXXXXXX',
'region' => 'eu-west-1'
)
)
)
);
?>
Then your main program can do:
require(_PS_ROOT_DIR_.'/override/aws/aws-config.php');
$s3 = S3Client::factory($s3options);

How do I send json data using Zend 2 http?

I've been struggling with this for a few days now - I need to send a set of data encoded in json to an api. I'm trying to use Zend 2 http to achieve this but I've had no luck so far. Here is what the manual for the api says :
Bulk Create Contacts
This command can be used to insert new numbers into your Textlocal contact groups
and will allow you to insert extra fields such as the contact's name.
Parameter Description
group_id ID of the group you’d like to store the numbers in. This
contacts A JSON object containing all the details for each contact.
Note: It is recommended to send this request via POST.
Sample Request
(POST Variables):
username: testing#Textlocal.co.uk
hash: 4c0d2b951ffabd6f9a10489dc40fc356ec1d26d5
group_id: 5
contacts: [{"number":"447123456789","first_name":"Bob","last_name":"Smith","custom1":"","custom2":"","custom3":""},{"number":"447987654321","first_name":"Sally","last_name":"McKenzie","custom1":"","custom2":"","custom3":""},{"number":"447000000000","first_name":"Ramesh","last_name":"Dewani","custom1":"","custom2":"","custom3":""}]
Ok so that's what it expects and this is my code so far :-
include 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
use Zend\Http\Client;
use Zend\Http\Request;
$data = array(
'username' => 'myUsername',
'hash' => 'myHash',
'group_id' => 123456,
'contacts' => json_encode(array('number'=>447123456789,'first_name'=>'Bob','last_name'=>'Smith','custom1'=>"",'custom2'=>"","custom3"=>""))
);
$uri = 'https://api.txtlocal.com/create_contacts_bulk';
$request = new Request();
$request->setUri($uri);
$request->setMethod('POST');
$request->getPost()->set($data);
$config = array(
'adapter' => 'Zend\Http\Client\Adapter\Socket',
'ssltransport' => 'ssl',
'sslcert' => '/etc/pki/tls/cert.pem',
'sslcapath' => '/etc/pki/tls/certs'
);
$client = new Client(null, $config);
$client->setEncType(Client::ENC_FORMDATA);
$response = $client->dispatch($request);
if ($response->isSuccess()) {
echo "the post worked!";
}else{
echo "the post failed";
}
That's not working and I'm sure there's I'm missing - here are the errors I get when I fire that script : -
[Thu Oct 24 09:29:47 2013] [error] [client] PHP Warning: Missing argument 2 for Zend\\Stdlib\\Parameters::set(), called in zend_test.php on line 24 and defined in Zend/Stdlib/Parameters.php on line 110
[Thu Oct 24 09:29:47 2013] [error] [client] PHP Notice: Undefined variable: value in Zend/Stdlib/Parameters.php on line 112
[Thu Oct 24 09:29:47 2013] [error] [client] PHP Fatal error: Uncaught exception 'Zend\\Http\\Client\\Exception\\RuntimeException' with message 'Cannot handle content type '' automatically' in Zend/Http/Client.php:1218\nStack trace:\n#0 Zend/Http/Client.php(858): Zend\\Http\\Client->prepareBody()\n#1 Zend/Http/Client.php(798): Zend\\Http\\Client->send(Object(Zend\\Http\\Request))\n#2 zend_test.php(30): Zend\\Http\\Client->dispatch(Object(Zend\\Http\\Request))\n#3 {main}\n thrown in Zend/Http/Client.php on line 1218
Any light you can shed or help you can give me would be greatly appreciated :)
Thanks in advance
Joe
You need to set the enc type when adding post data to the request object (this isn't very clear in the docs). Also, the example from the API manual implies that it's only the contacts that need to be JSON, you've encoded the whole array.
Try this:
$data = array(
'username' => 'myUsername',
'hash' => 'myHash',
'group_id' => 123456,
'contacts' => json_encode(array('number'=>447123456789,'first_name'=>'Bob','last_name'=>'Smith','custom1'=>"",'custom2'=>"","custom3"=>""))
);
$request = new Request();
$request->setUri('https://api.txtlocal.com/create_contacts_bulk');
$request->setMethod('POST');
$request->getPost()->fromArray($data);
$client = new Client();
$client->setEncType(Client::ENC_FORMDATA);
$response = $client->dispatch($request);
if ($response->isSuccess()) {
echo "the post worked!";
}else{
echo "the post failed";
}
Edit: In order to verify that the SSL certificate is valid, the HTTP client needs the path to the CA certificates on your web server.
You pass this in as a config option to the Http client instance:
$client = new Client(null, array(
'sslcapath' => '/etc/ssl/certs'
));
That's the path on Ubuntu, you'll need to find out what it is for your server.
By the way, instead of long constructions of Request, Response and Client classes please conider static Client usage:
http://framework.zend.com/manual/2.0/en/modules/zend.http.client-static.html
use Zend\Http\ClientStatic;
// Simple GET request
$response = ClientStatic::get('http://example.org');
// More complex GET request, specifying query string 'foo=bar' and adding a
// custom header to request JSON data be returned (Accept: application/json)
$response = ClientStatic::get(
'http://example.org',
array( 'foo' => 'bar' ),
array( 'Accept' => 'application/json')
);
// We can also do a POST request using the same format. Here we POST
// login credentials (username/password) to a login page:
$response = ClientStatic::post('https://example.org/login.php', array(
'username' => 'foo',
'password' => 'bar',
));

SOAP error; missing parameter despite given arguments

The following soap request
$response = $this->client->__soapCall('Match', array('word' => 'test', 'strategy' => 'exact'));
yields the error
Uncaught SoapFault exception: [soap:Client] Parameter not specified (null)
Parameter name: word
How can this be? I specified the word parameter in the request, didnt I? Why doesn's the server recognize it?
The service I want to use is an online dictionary webservive
Generally you need to wrap the arguments in a double array:
$response = $this->client->__soapCall('Match', array(array('word' => 'test', 'strategy' => 'exact')));
It reads a bit nicer if you
$aParams = array('word' => 'test', 'strategy' => 'exact');
$response = $this->client->__soapCall('Match', array($aParams));
Or you can simply call the Match function directly
$aParams = array('word' => 'test', 'strategy' => 'exact');
$response = $this->client->Match($aParams);
Or, as a final resort, use the HTTP GET option: http://services.aonaware.com/DictService/DictService.asmx?op=Match
Should get you on the road again.

Categories