Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

curl print

Hello,

I get this error message and cannot understand why? array_merge(): Argument #2 is not an array

public function output_json()

    {
    //
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.github.com/users/petanikode",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_TIMEOUT => 30000,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            // Set Here Your Requesred Headers
            'Content-Type: application/json',
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        //di jadiin array
        $json_arr = json_decode($response, TRUE);
    }

    return $json_arr;

    }

public function print_json()
{

    $data = array();
    $data = $this->output_json();

    return view('show', $data);

}
0 likes
14 replies
Sinnbeck's avatar

Do you get a stacktrace telling your where the error occurs?

RichardStyles's avatar

If there is an error $json_arr has not been initialised and isn't an array.

If the repsonse from the curl request able to be json_decoded to an array - is this an array at this point or not?

davy_yg's avatar
Level 27

I think $json_arr should be an array already.

JamesFreeman's avatar

Add $json_arr = []; after

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
davy_yg's avatar
Level 27

I still have the same error:

array_merge(): Argument #2 is not an array

JamesFreeman's avatar

Try changing: return view('show', $data); to return view('show', ['data' => $data]);

davy_yg's avatar
Level 27

show.blade.php

<html>

<h1>JSON</h1>

@foreach($data as $data_json){

    {{ $data_json }}
}
@endforeach

Invalid argument supplied for foreach() (View: D:\xampp72\htdocs\curl_laravel\resources\views\show.blade.php)

JamesFreeman's avatar

Can you see what the CURL request is setting the $json_arr to? It should be set to an array if you have done my sets from earlier?

davy_yg's avatar
Level 27

I have done this:

    ...

    $json_arr = [];

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        //di jadiin array
        $json_arr = json_decode($response, TRUE);
    }

    return $json_arr;

    }

I try this on the view.

show.blade.php

<html>

<h1>JSON</h1>

    <? var_dump($data); ?>
    @foreach((array)$data as $data_json){
    
        {{ $data_json['avatar_url'] }}
    }

    @endforeach

</html>

It prints JSON but only the title. Does this means $data is empty?

I wonder if you need to install anything to run curl_init() ?

Snapey's avatar

Your request is failing because you are not providing a user agent string in the headers

https://developer.github.com/v3/#user-agent-required

add user agent to the headers

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.github.com/users/petanikode",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_TIMEOUT => 30000,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            // Set Here Your Requesred Headers
            'Content-Type: application/json',
            'User-Agent: Some-agent-string-here',
        ),
    ));
davy_yg's avatar
Level 27

show.blade.php

<html>

<h1>JSON</h1>

    <? var_dump($data); ?>
    @foreach((array)$data as $data_json){
    
        {{ $data_json->avatar_url }}
    }

    @endforeach

Trying to get property 'avatar_url' of non-object (View: D:\xampp72\htdocs\curl_laravel\resources\views\show.blade.php)

Snapey's avatar

Is this the 4th or 5th issue in this question?

Do we build the whole application this way?

Snapey's avatar

Its an array, not an object

{{ $data_json['avatar_url'] }}

next?

Snapey's avatar
Snapey
Best Answer
Level 122

oh, and its an array of attributes. One of which is the avatar_url

Lose the foreach loop

<html>

<h1>JSON</h1>

{{ $data->avatar_url }}

  

Please or to participate in this conversation.