I am working to get response from dutchie GrapghQL api using PHP, These are working fine with POSTMAN , but when I am trying to get them work with below Library , its not working at all, and there is very less documentation available for dutchie(php graghql)
https://github.com/webonyx/graphql-php/
Before I have worked on rest apis with php MySQL, its totally new thing to me so not getting how get it work
Postman working image
<?php
include("library/graphQL/vendor/autoload.php");
use GraphQL\Client;
use GraphQL\Exception\QueryError;
$client = new Client(
'https://plus.dutchie.com/plus/2021-07/graphql',
["Authorization" => "public- eyJhbGciOiJIUzI1NiJ9."]
);
// Create the GraphQL mutation
$gql = (new Mutation('Ping'))
->setSelectionSet(
[
'id',
'time',
]
);
// Run query to get results
try {
$results = $client->runQuery($gql);
}
catch (QueryError $exception) {
// Catch query error and desplay error details
print_r($exception->getErrorDetails());
exit;
}
Display original response from endpoint
var_dump($results->getResponseObject());
Display part of the returned results of the object
var_dump($results->getData()->pokemon);
Reformat the results to an array and get the results of part of the array
$results->reformatResults(true);
print_r($results->getData()['data']);
Error I am getting
Fatal error: Uncaught Error: Class 'Mutation' not found in
C:\xampp\htdocs\dispoapi\index.php:21 Stack trace: #0 {main}
thrown in C:\xampp\htdocs\dispoapi\index.php on line 21
For me, the fix was to include the class, like this:
$gql = (new GraphQL\Mutation('Ping'))
Notice the "GraphQL\" in front of "Mutation"? If you don't setup an alias for "Mutation", then that has to be added in front of it.
Otherwise, this has to come after the "GraphQL" library include:
use GraphQL\Mutation;
Related
I have a Cest with a _before() that has the following code in it to authenticate (API testing):
// Log a user in
$I->haveHttpHeader('Accept', 'application/json');
$I->sendPOST('/authentication/login', ['username' => $this->username, 'password' => $this->password]);
$this->api_token = json_decode($I->grabResponse())->token;
When I run the test I get the following error:
[PHPUnit_Framework_Exception] Trying to get property of non-object
Scenario Steps:
3. $I->grabResponse() at tests/api/ApiBase.php:19
2. $I->sendPOST("/authentication/login",{"username":"user#examplecom","password":"abc123"}) at tests/api/ApiBase.php:17
1. $I->haveHttpHeader("Accept","application/json") at tests/api/ApiBase.php:16
tests/api/ApiBase.php:19 is $this->api_token = json_decode($I->grabResponse())->token;
It appears as if $I is no longer an object, but I have confirmed that it is still an instance of ApiTester. So the only thing I can think of is the call to the property of a non-object is somewhere deeper down.
How can I tell where? I'm running this with the --debug switch enabled.
[EDIT] This isn't a problem with the json_decode. If I move $I->grabResponse() up then the error will be on that line.
Validate response before using it.
seeResponseJsonMatchesJsonPath checks if the element is present in json response.
$I->seeResponseJsonMatchesJsonPath('$.token');
Also it could be useful to use grabDataFromResponseByJsonPath method instead of parsing it yourself.
$this->api_token = $I->grabDataFromResponseByJsonPath('$.token')[0];
[0] is necessary because grabDataFromResponseByJsonPath returns a list of matching items.
You may need to install flow/jsonpath library to get JsonPath methods working if it isn't installed yet.
Pretty new to laravel, so I'm not exactly sure how it handles errors and how best to catch them.
I'm using a 3rd party game server connection library that can query game servers in order to pull data such as players, current map etc..
This library is called Steam Condenser : https://github.com/koraktor/steam-condenser
I have imported this using composer in my project and all seems to be working fine, however I'm having trouble with catching exceptions that are thrown by the library.
One example is where the game server you are querying is offline.
Here is my code:
public function show($server_name)
{
try{
SteamSocket::setTimeout(3000);
$server = server::associatedServer($server_name);
$server_info = new SourceServer($server->server_ip);
$server_info->rconAuth($server->server_rcon);
$players = $server_info->getPlayers();
$total_players = count($players);
$more_info = $server_info->getServerInfo();
$maps = $server_info->rconExec('maps *');
preg_match_all("/(?<=fs\)).*?(?=\.bsp)/", $maps, $map_list);
}catch(SocketException $e){
dd("error");
}
return view('server', compact('server', 'server_info', 'total_players', 'players', 'more_info', 'map_list'));
}
If the server is offline, it will throw a SocketException, which I try to catch, however this never seems to happen. I then get the error page with the trace.
This causes a bit of a problem as I wish to simply tell the end user that the server is offline, however I cannot do this if I can't catch this error.
Is there something wrong with my try/catch? Does laravel handle catching errors in this way? Is this an issue with the 3rd party library?
A couple things:
Does the trace lead to the SocketException or to a different error? It's possible that a different error is being caught before the SocketException can be thrown.
Your catch statement is catching SocketException. Are you importing the full namespace at the top of your PHP file? use SteamCondenser\Exceptions\SocketException;
Also for debugging purposes, you could do an exception "catch all" and dump the type of exception:
try {
...
}catch(\Exception $e){
dd(get_class($e));
}
If you still get the stack trace after trying the above code, then an error is being thrown before the try/catch block starts.
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"));
...
I'm using Laravel 4.2 and want to get logs out as JSON. Laravel uses Monolog, so I've configured the JSON formatter like so:
$logHandler = new Monolog\Handler\StreamHandler(Config::get('app.logFile'), Monolog\Logger::DEBUG);
$logHandler->setFormatter(new Monolog\Formatter\JsonFormatter);
Log::getMonolog()->pushHandler($logHandler);
The problem is that stack traces are included as part of the message string, like so:
{
"message": "exception 'Exception' with message 'Socket operation failed: Host name lookup failure' in /var/www/vendor/clue/socket-raw/Socket/Raw/Socket.php:388\nStack trace:\n#0 /var/www/vendor/clue/socket-raw/So..."
}
Can someone point me in the right direction to make the stack trace its own separate array in the json?
Long overdue update:
The primary problem is that Laravel, and lots of code following its example, tries to log the exception by itself, e.g., Log::error($e). This is the wrong way to log an exception with Monolog. That first parameter is supposed to be a simple message string. In processing it, Monolog\Logger::addRecord() explicitly casts the message to a string:
$record = array(
'message' => (string) $message,
//...
);
If $message is actually an exception, you get the whole stack trace as a string. Unfortunately, this is what Laravel's default exception handler does.
The correct way to get a stack trace as an array is to pass the exception in as context so that it's available non-stringified to the formatter. For instance:
Log::error($e->getMessage(), ['exception' => $e]);
Something equivalent is what you need to put in your custom exception handler for Laravel, and then you can use a proper JSON formatter and get what you except.
I'm using the google-api-php-client (https://github.com/google/google-api-php-client)
When I try to connect to the Freebase service I get the following error:
Fatal error: Call to undefined method Google_Service_Freebase::call()
Here's a snippet of my code:
$freebase->search(array('automotive');
The search function in the Freebase service looks like this:
public function search($optParams = array())
{
$params = array();
$params = array_merge($params, $optParams);
return $this->call('search', array($params));
}
It calls to an internal function call, but it doesn't exists in the class..
Anyone suggestions?
I took a look at the lib code (which is in beta and changed a lot since the last version), I think that return line should be:
return $this->base_methods->call('search', array($params));
I can't try this now, but if you do and it work, you could submit a bug report on their Github (I saw your post there :).