So I have installed the Guzzle library version 6 according to TeamUp calendar documentation. However, when I try to run the code below I get
Fatal error: Call to undefined method GuzzleHttp\Psr7\Response::isSuccessful()
code:
<?php
include 'vendor/autoload.php';
define('API_KEY','****ww9d5ea2b0540ba1e02c08100b0e5**');
$client = new GuzzleHttp\Client(['headers' => ['Teamup-Token' => API_KEY]]);
$res = $client->get('https://api.teamup.com/ks************/events?startDate=2016-08-21&endDate=2016-08-25');
if ($res->isSuccessful()) {
echo $res->getBody();
// {"event":{ ... }}
}
Shouldn't be contained in Library?
Anyone?
Yes, there is no method isSuccessful.
By default Guzzle will throw exception if server return error
http://docs.guzzlephp.org/en/latest/quickstart.html
A GuzzleHttp\Exception\ServerException is thrown for 500 level errors
if the http_errors request option is set to true.
A GuzzleHttp\Exception\ClientException is thrown for 400 level errors
if the http_errors request option is set to true.
In the event of a networking error (connection timeout, DNS errors,
etc.), a GuzzleHttp\Exception\RequestException is thrown.
Anyway, you can check the status code of the response using
$res->getStatusCode();
The upgrade notes from Guzzle 5.0 to Guzzle 6.0 say:
GuzzleHttp\Message\Response::isSuccessful() and other related methods have been removed. Use getStatusCode() instead.
Related
I' ve a problem with Soap (I'm using it for the first time!).
I'm trying to solve a problem with SOAP, used to communicate between my site and another.
In SoapServer (on my site) I have a function called say_hello() that takes no argument and returns simply "hello", if I call it from a SoapClient, also with an argument, it works well.
The problem appears if the argument passed is "SELECT everythingyouwanthere FROM otherthingifyouwant". I don't know why but it returns "PHP Fatal error: Uncaught SoapFault exception: [HTTP] Not Found" (404 error).
This started to happen suddenly (or, at least, I don't know the causes). On the server PrestaShop is installed.
Thanks in advance!
P.S. Sorry for my bad english!
try to pass the argument through an array.
Instead of
...
$client = new SoapClient(WSDL);
$client->say_hello("SELECT everythingyouwanthere FROM otherthingifyouwant");
...
try
...
$client = new SoapClient(WSDL);
$client->say_hello(array("parameter_name" => "SELECT everythingyouwanthere FROM otherthingifyouwant"));
...
<?php
$this->client = new \GuzzleHttp\Client;
try {
$response = $this->client->request('get', $url);
$result = json_decode($response->getBody());
Log::save($result);
} catch (RequestException $e) {
Log::save($e->getMessage());
}
If the target page has internal server error 500 situation, I will get the error message Client error: 404, actually the target page has show what the bug is if I use the browser to open it, I want to save those PHP error message to log, but I don't know how to got when using Guzzle.
There are a couple of different ways you can proceed:
If you are using Guzzle 6+ and you are using the default
HandlerStack (creating the client with no handler new \GuzzleHttp\Client(); the default behaviour will be for the request
to throw a GuzzleHttp\Exception\ServerException exception. In this case, all you have to do is wrap your request in a try / catch block (as shown above).
If you are manually creating your
HandlerStack $stack = new \GuzzleHttp\HandlerStack(); $client = new \GuzzleHttp\Client(['handler' => > $stack,]); then the \GuzzleHttp\Middleware::httpErrors middleware has not been loaded onto the stack, and you will have to trap it within your own middleware or by passing a callable to the 'on_headers' request option.
References:
Guzzle Request Options - http_errors
Guzzle Handlers and Middleware
Guzzle Request Options - on_headers
Can anyone explain what all errors can App::error of Laravel handles..?
For eg :
[404] unable to access the url
[500] internal server error
If I have a db connection error or any missing parameter error it doesn't come under this class. How can I handle those major errors..?
Please help in listing all the possible cases..
The error handling isn't specifically tied to HTTP status codes.
App::error handles any uncaught exceptions. A not found error is just a NotFoundHttpException.
http://laravel.com/docs/4.2/errors#handling-errors
A 404 exception can be easily caught with this shortcut method:
App::missing(function($exception)
{
// Example response
return Response::view('errors.missing', array(), 404);
});
http://laravel.com/docs/4.2/errors#handling-404-errors
If you don't use the App::missing syntax, a not-found type of exception should bubble up to the App::error handler.
I'm using the amqp extension in pecl 1.0.3, compiled with 2.7.1 rabbitmq.
I'm trying to get a basic consumer/producer example working, but I keep getting errors. There's very little php documentation on this extension and a lot of it seemed to be outdated or wrong.
I used the code a user posted, but can't seem to get the consumer part working
Connection:
function amqp_connection() {
$amqpConnection = new AMQPConnection();
$amqpConnection->setLogin("guest");
$amqpConnection->setPassword("guest");
$amqpConnection->connect();
if(!$amqpConnection->isConnected()) {
die("Cannot connect to the broker, exiting !\n");
}
return $amqpConnection;
}
Sender:
function amqp_send($text, $routingKey, $exchangeName){
$amqpConnection = amqp_connection();
$channel = new AMQPChannel($amqpConnection);
$exchange = new AMQPExchange($channel);
$exchange->setName($exchangeName);
$exchange->setType("fanout");
if($message = $exchange->publish($text, $routingKey)){
echo "sent";
}
if (!$amqpConnection->disconnect()) {
throw new Exception("Could not disconnect !");
}
}
Receiver:
function amqp_receive($exchangeName, $routingKey, $queueName) {
$amqpConnection = amqp_connection();
$channel = new AMQPChannel($amqpConnection);
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$queue->bind($exchangeName, $routingKey);
//Grab the info
//...
}
Then sending it:
amqp_send("Abcdefg", "action", "amq.fanout");
And Receiving it:
amqp_receive("amq.fanout","action","action");
I keep getting a problem running the script and points to the amqp receive:
PHP Fatal error: Uncaught exception 'AMQPQueueException' with message 'Server channel error: 404, message: NOT_FOUND - no queue 'action' in vhost '/'' in /home/jamescowhen/test.php:21
Can anyone point me to the right direction? The whole sample is from a user note here:
http://www.php.net/manual/en/amqp.examples.php#109024
The exception seems to be caused by your queue not being declared (as the error message describes 404 - the queue 'action' was not found). The reason why the example works for the original poster is probably because he already has declared the queue earlier, without realizing that it's missing in his example.
You can declare the queue by calling ->declare() on the queue object. You'll also have to do this with the exchange object, unless you're certain that it already exists when you attempt to hook the queue onto it.
try
{
$client = new \SoapClient($wsdlUrl, array(
'cache_wsdl' => 0, 'exceptions' => true, 'trace' => true));
$client->getPage($parameter);
}
catch(\Exception $e)
{
die("exception");
}
Okay this is what executes the SOAP request. $wsdlUrl holds the WSDLs URL and $parameter keeps an XML document.
And everything works like a charm!
Now I just add few more nodes to the XML document in $parameter and I get a fatal error.
This is not so weird necessarily, but what is weired is the combination of following three observations:
1) exceptions is set to true .... but no Exception is thrown / this was b/c I forgot to place a backslash before Exception in the catch statement.
2) instead an error is logged:
PHP Fatal error: SoapClient::SoapClient():
'uri' option is required in nonWSDL mode in /var/w[...]
3) but the WSDL url is provided and of course valid, as all works again just fine after skipping the adding of the new nodes. they don't affect the wsdl parameter.
Okay, I tracked down the source of the problem. There is no satisfying answer really to that question but few things to learn! :)
The important thing is that the request has been sent and processed by the SOAP server.
But the server responds with an error.
So, obviously, even though a fatal error regarding no wsdl and no connection parameter is triggered doesn't mean no request has been sent - which is very counter logical in my opinion b/c if no wsdl is provided and no connection params as well, then how could the request be executed?
EDIT
There is another bug in the code. I forgot to place a backslash before Exception! Without it's not referring to the Exception class.
Then an exception is thrown and caught.