1

These are my controllers

<?php

public static function getAccessToken()
{
    $url = 'http://api.tech/oauth/authenticate';
    $query = [
        'grant_type' => 'client_credentials',
        'client_id' => 'E3PuC',
        'client_secret' => 'IhvkpkvMdAL7gqpL',
        'scope' => 'bookings.read,images.create,images.read,images.update,locations.read,rates.read,rates.update,reports.read,reviews.read,rooms.create,rooms.delete,properties.read',
    ];

    $client = new Client();
    $response = $client->get($url, ['query' => $query]);
    $content = json_decode($response->getBody()->getContents());

    if ($content) {
        return $content->access_token;
    } else {
        return null;
    }
}

public function getReviews()
{
    $client = new Client();
    $access_token = $this->getAccessToken();
    $url = 'http://api.tech/hotels/88244/reviews';
    $query = [
        'access_token' => $access_token,
    ];

    $response = $client->get($url, ['query' => $query]);
    $content = json_decode($response->getBody()->getContents());

    if ($content->status == 'success') {
        // return $content->access_token;
        return $content->data;
        //    return $response;
    } else {
        return null;
    }

}

public function index()
{
    $content = $this->getReviews();
    return view('channel.channel', [
        'content' => $content
    ]);
}

When i try to output the content in my blade as a link, it says ---

htmlspecialchars() expects parameter 1 to be string, array given

and this is my blade file

<a href="{{$content}}">This</a>

It also throws an error when i try to output it like thus

{{$content}}

Please How can i solve the error My question isn't a duplicate cause once i dd it shows an array and i want a link to show the array on a different page

Nic3500
  • 8,144
  • 10
  • 29
  • 40
Paul
  • 263
  • 4
  • 21
  • 1
    Possible duplicate of [Laravel - htmlspecialchars() expects parameter 1 to be string, object given](https://stackoverflow.com/questions/43217872/laravel-htmlspecialchars-expects-parameter-1-to-be-string-object-given) – Indra May 31 '18 at 13:46
  • https://stackoverflow.com/questions/40045920/laravel-5-3-htmlspecialchars-expects-parameter-1-to-be-string try {{dd($content)}} it's probably a formating issue – Indra May 31 '18 at 13:47
  • 1
    your $content is an array. do {{dd($content)}} and see – tech2017 May 31 '18 at 13:47
  • I believe the best practise is to use: `$content = $client->get($url, ['query' => $query])->json());` Otherwise, just use `$content = json_decode($response->getBody());` – Popmedic May 31 '18 at 13:58
  • @Indra when i do dd it shows an array, but u want to show the variable as a javascript object on a different page when clicked on the link – Paul May 31 '18 at 13:59
  • @Popmedic after that please how will i echo it on a different page in my blade file i.e Link – Paul May 31 '18 at 14:01
  • @IrueneAdokiye I am talking about in your controller, you are getting the contents of the body. By the OAuth standard you should not be getting an array back, rather an Object like: `{ token_type: "bearer", access_token: "ACCESS_TOKEN", refresh_token: "REFRESH_TOKEN", expires_in: SECONDS_TO_EXPIRATION }` so something is wrong with the response if it is an array. I usually don't use `$response->getBody()->getContents()` but `$response->getBody()` to pass into `json_decode` **OR** `$client->get($url, ['query' => $query])->json())` – Popmedic May 31 '18 at 14:05
  • Ok after doing this `$client->get($url, ['query' => $query])->json())` How will i output it through a link – Paul May 31 '18 at 14:10
  • Usually the access token is placed in the header as `Authorization: bearer ACCESS_TOKEN` but it depends on how you are implementing it. The link would just be the link... – Popmedic May 31 '18 at 14:20
  • Ok if I understand you correctly, if i put what is returned from `$client->get($url, ['query' => $query])->json())` in a `a href` link it will work – Paul May 31 '18 at 14:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172173/discussion-between-popmedic-and-iruene-adokiye). – Popmedic May 31 '18 at 14:31
  • You have an array so you have to loop through it and show every entry. Otherwise just pass it as a json object and pass it to a JavaScript variable and loop through it in JavaScript. To loop through it in a blade: @foreach($content as $value) {{$value}} @endforeach – Indra Jun 01 '18 at 14:55

1 Answers1

0

Try Using: {! $content !} Or Use: @json($content)

  • Please add some explanation to your answer such that others can learn from it. Wouldn't it look pretty strange to have a json encoded string within the `href` attribute? – Nico Haase Sep 27 '21 at 09:55