Viewed   531 times

I use Guzzle to make an XML request to an external API in the back-end.

This is where I create the Guzzle client:

$client = new Client(); //GuzzleHttpClient

This is where I make the request:

$request = $client->request( 'GET', 'This is where I put the URL');

This is where I get the Guzzle response and try to parse it:

$xml = $request->getBody()->getContents();
$xml = new SimpleXMLElement($xml);
$xml = simplexml_load_string($xml);

If I do this:


I receive this in return:

SimpleXMLElement {#240 ?
  +"StatusCode": "1"
  +"StatusDescription": "Duplicated request"
  +"MerchantId": "***"
  +"Company": "***"
  +"CompanyCity": "***"
  +"CompanyPhone": "***"
  +"CompanyDbaName": "***"
  +"Balance": "99965"
  +"TransDate": "2017-10-07"
  +"TransTime": "06:58:48"
  +"ProductVer": "***"
  +"PromoVer": object
  +"SoftVer": object
  +"InvoiceNumber": object

My problem is that I don't know how to parse this. I want to get the 1 in StatusCode.

If I do this:


I receive this:

SimpleXMLElement {#242 ?
  +0: "1"

How do I get just 1 ?



The var_dump() output is correct. $xml->StatusCode is a SimpleXMLElement instance. This is of course needed in case you have to, for example, add a child element to it:

$xml->StatusCode->addChild("test", "value");

If $xml->StatusCode contained only the value of the element rather than an instance of SimpleXMLElement, you wouldn't be able to do any modifications on the loaded XML.

So, what you need to do, is cast the value of StatusCode to a string. There are various ways of doing this:

var_dump($xml->StatusCode); // returns SimpleXMLElement instance
var_dump((string)$xml->StatusCode); // explicitly
var_dump($xml->StatusCode->__toString()); // explicitly, calling the magic function
echo $xml->StatusCode; // implicitly

Some demos

Wednesday, October 19, 2022

I managed to get this to work using Artisan::output(), which returns the output of the latest command.

Route::get('/test', function()
    Artisan::call('testCommand', array());

    return Artisan::output();

should do it for you.

Tuesday, December 6, 2022

The 1st question is whether you really know what you are doing. If you take data from database and then filter it just to take some elements it's definitely not the best way because you can take from database for example 100000 records just to finally have only 2 elements and it will kill your application performance.

But assuming you really want to filter using Support collection, there is no where together with LIKE because you are just filtering an array. If you want to use something similar to like instead of:

$collection = $collection->where('name', $value);

you can use:

$collection = $collection->reject(function($element) use ($value) {
    return mb_strpos($element->name, $value) === false;

depending on what you really have in collection instead of $element->name you might need to use $element['name']

Friday, September 9, 2022

I also found a more convenient solution:

$relationtypes = RelationType::pluck('name', 'id')->map(function ($item, $key) {
    return trans('labels.' . $item . '');

Passing this to your view, you can use:

{!! Form::select('relationtypes[]', $relationtypes, 
    isset($relation) ? $relation->relationtypes->pluck('id')->toArray() : 0, ['class' => 'form-control']) !!}

Hope this helps other people!

Friday, December 23, 2022

I don't think it's possible to use only function when you have code in your classes. Well, you could try with extending Blade but it's too much.

What you should do is creating one extra file, for example appHelpershelpers.php and in your composer.json file put:

"autoload": {
    "classmap": [
    "psr-4": {
        "App\": "app/"
    "files": ["app/Helpers/helpers.php"] // <- this line was added

create app/Helpers/helpers.php file and run

composer dump-autoload

Now in your app/Helpers/helpers.php file you could add those custom functions for example like this:

if (! function_exists('fooBar')) {
   function fooBar() 
      return AppHelpersCustomHelper::fooBar();

so you define global functions but in fact all of them might use specific public methods from some classes.

By the way this is exactly what Laravel does for its own helpers for example:

if (! function_exists('array_add')) {
    function array_add($array, $key, $value)
        return Arr::add($array, $key, $value);

as you see array_add is only shorter (or maybe less verbose) way of writing Arr::add

Friday, August 5, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :