Android how to catch an exception thrown from php rest - php

I have a PHP RESTful API, which returns JSON objects.
I have an Android client requesting REST requests from that API
Till now if there was a problem in the server side then the API returned a booleanic response "false"
I want to change the flow of the API, so instead of sending a JSON response containing "false" it will throw and Exception, and I want the android side to catch the exception.
So basically, i want something like the following Android code:
try {
php_api.getUsers();
} catch (Exception ex) {
// TODO
}
Can it be done?
Will any PHP exception be caught in this way?

I haven't worked with Java but I don't think that you can do that.
When PHP throws an exception, it will still produce an output of some sort, depending on how you set up exception handling.
It could show the exception message with a stack trace or something similar.
But on the client (your Java app) side, this is nothing more than text.
However, a rest response should always have headers that you can check. Take a look at this: http://blog.zenika.com/index.php?post/2011/05/18/Error-handling-with-REST
By following what is written there, your Java-to-PHP api would always read the response headers before returning the content. If a 500 or 400 showed up, you'd throw a java exception from the api which you could catch in the code that uses it.
If your PHP side is not sending the headers you want, you can always override PHP's error/exception handling to produce custom output (including headers).

Related

Laravel 5: Guzzle -> getStatusCode on Exception?

I am programming a little application for myself. This application is calling to different websites with the package Guzzle.
However, I want to store every request in my database with the time and the request duration time and the request status code I get. The problem I am facing here right now is that I don't know how to get the http status code when the request fails..
This is my code so far:
$client = $this->getGuzzleClient();
$request = $client->post($url, $headers, $value);
try {
$response = $request->send();
return $response->getBody();
}catch (\GuzzleHttp\Exception\RequestException $e){
dd(array($e, $e->getResponse()));
}
The $e->getResponse() returns null. I also tried to use $e->getStatusCode() or $e->getRequest()->getStatusCode(). Both are not working...
To be absolutely sure the request is valid and I deal with a real exception I call to this website https://httpstat.us/503. This returns a 503 http status code...
So, how can I get the http status code? Do you guys have any idea?
Kind regards and Thank You!
If you catch a ServerException you are catching a 5xx, if the code execution enters there Guzzle has received a 5xx. If you catch a RequestException that includes network errors too. If the code execution enters on the RequestException but does not on the ServerException means that for Guzzle is not a 5xx error but a network error.
$errorstatuscode=$exception->status;
// to get error code from Excetion Object

Laravel forcing json response for api

I'm building an api at my company using laravel.
The problem I'm encountering is that if you send an api request without defining the correct header with the request you will get html back if there is a failure e.g. authorization failure or findOrFail() failure.
My thinking is that you never want to return html (even if the user has the wrong header).
I have a couple of solutions. In BeforeMiddleware.php I can manually insert a header into the request such as:
// Check if we are on an api route
$apiRoute = strncmp($uri, '/api/', 5) == 0;
// Insert the request header to force json response
if ($apiRoute){
$language = $request->header->add('Accept', 'application/json');
}
The 2nd solutions would be to throw an error if they don't have the correct header.
What would be the best way to enforce a json response, what is a good practice for handling api responses in laravel?
Once you detected that you are on your api path you are out of the woods and can indeed tackle your problem in the app\Exceptions\Handler.php file like suggested on How do you force a JSON response on every response in Laravel?.
For an open source project I created JSON exception objects by Microsoft format as output, but you can choose the jsonapi format (http://jsonapi.org/examples/#error-objects-basics) as you like:
https://github.com/StadGent/laravel_site_opening-hours/blob/develop/app/Exceptions/Handler.php
(note that on this implementation it is indeed depending from the headers, but you can use your path detection I think)

SoapClient call returns 500 Internal Server Error

I am trying to connect to an API using PHP and its built-in SoapClient. I have checked against the url I was given through the ill-formatted documents the client gave and $client->__getFunctions() returns a list of three functions. HelloWorld($name), which responds with Hello ~name~, shows me that I am communicating with the server through the SoapClient call and the URL is correct.
However, when I try to access one of the other methods that __getFunctions() gives me, even after copy/pasting the XML from the docs and putting in my own credentials, I am still being given an Internal Server Error faultstring and 500 as faultcode from the SoapFault object.
I am sure that it is my own XML string that is causing the issue but I cannot for the life of me figure out how. Reaching out to the API provider directly hasn't proven helpful. This is my first time dealing with Soap/Web Services so I am unsure of where to go from here.
I did wget http//xxx.xxx.xxx?wsdl and it returned me what looks like a valid XML response, the same one I get when I go directly to the url in the browser. What should I be looking into in order to solve this issue? All of the past API's I've dealt with have been JSON/RESTful so I feel out of my element trying to debug PHP errors.
Edit
I have slowly deleted parts of my method call and parts of my XML string, trying to trigger a different error or something in order to find what I need to fix. What I have found is that by not passing in my XML string, I get a valid response from the $client->FunctionCall(...). It's an "this isn't right" message but it's a message! In fact, passing that function ANYTHING for the xml parameter causes the 500 http faultcode/faultstring. Does this mean that my XMl is poorly formatted or does it mean that there is an issue on their end handling requests?
Second Edit
If I make my $client decleration as follows, I get the faultstring Could not connect to host
$opts = array(
'ssl' => array('ciphers'=>'RC4-SHA')
);
$client = new SoapClient($CREDS['orderingWSDL'], array (
"encoding"=>"ISO-8859-1",
'stream_context' => stream_context_create($opts),
'exceptions'=>true,
));
I am getting more confused the longer I try to fix this.
Sometimes a 500 status coming from a SOAP service could be a SoapFault exception being thrown. To help your troubleshooting, you'll want to be able to inspect both your request XML, and the response XML.
Put your code in try/catch blocks, and use $client->__getLastRequest() and $client->__getLastResponse() to inspect the actual XML.
Example:
$client = new SoapClient('http//xxx.xxx.xxx?wsdl', array('soap_version'=>SOAP_1_1,'trace' => 1,'exceptions' => true));
try {
$response = $client->someFunction();
var_dump($response);
} catch (Exception $e) {
var_dump($e->getMessage());
var_dump($client->__getLastRequest());
var_dump($client->__getLastResponse());
}

SimpleXMLElement - HTTP status code?

I'm working with the PHP Steam Condenser library, to grab some Steam details about a user. I've successfully implemented this and everything is working. However I started to notice an error being thrown every so often, from Steam Condenser.
What's happening is, when I call the SteamId class with a param, the library generates a URL and makes a request to it, using SimpleXMLElement (as can be seen here). Now most of the time, the URL returns XML and so my application works fine, however every so often Steam throws back a 503 Service Unavailable back, causing it to fail.
try {
return #new SimpleXMLElement($url, 0, true);
} catch (Exception $e) {
$errorMessage = "XML could not be parsed: " . $e->getMessage();
if ((float) phpversion() < 5.3) {
throw new SteamCondenserException($errorMessage, 0);
} else {
throw new SteamCondenserException($errorMessage, 0, $e);
}
}
In my cause, the PHP version is correct, so it throws the bottom custom Exception:
SteamCondenserException
XML could not be parsed: String could not be parsed as XML
/home/user/public_html/acme/vendor/koraktor/steam-condenser/lib/steam/community/XMLData.php (Line 38)
Although this is technically the correct exception, it isn't very meaningful since the request was just "Unavailable" and therefore couldn't gather the XML.
How would I go about editing this code to first check what the status code of the request is, if it's a 302 or 200 (since it redirects), then proceed to check the XML, otherwise if it's a 503, respond with a more genuine error (The Steam Community API is currently down) - or something.
I've Google'd my ass off, but can't see anything. Ideally I'd like it all to be done in the same request, since Steam can be a little slow sometimes.
Cheers
Don't use SimpleXMLElement to also do the HTTP request.
Use curl (because it's fast) to fetch the XML plain-text and SimpleXML to parse it.
That way you can separate service availability from transmission errors (or XML errors).

Sending a call to facebook graph api does not return anything when an error occurs

I am using graph api to get a list of albums of a user. Now in case the user signs out or has changes password or any error condition, I do not get any response whereas some error code should be returned. The graph API url when opened in the browser shows the error but on using file_get_contents() i get error 400. On using curl, I get an empty response. Is there any way that I can find out when an error occurs and the reason for it?
The graph API url when opened in the browser shows the error but on using file_get_contents() i get error 400.
Well, that is the HTTP status code that Facebook sends with Oauth errors – nothing wrong about that.
But file_get_contents by default does not show you the actual HTTP response body content in case of an HTTP status codes that signals an error …
… which is just another reason, why one should use the official PHP SDK, which handles all of that nicely and presents you with an exception that tells you exactly what is up.
So do it, please.
(Even with file_get_contents one can get the body content of the response, when setting a “stream context” with the according option first. But using the PHP SDK is way simpler, so again: Please do it.)
I used curl instead of file_get_contents and the issue got resolved.
file_get_contents() gave me error many times when I put https instead of http or If I forgot to put any of them.
Your URL from which you get the content must have "http" at the starting, like:
file_get_contents("http://www.mysite.com");
If this solves it?

Categories