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);
}
Do you get a stacktrace telling your where the error occurs?
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?
I think $json_arr should be an array already.
Add $json_arr = []; after
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
I still have the same error:
array_merge(): Argument #2 is not an array
Try changing: return view('show', $data); to return view('show', ['data' => $data]);
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)
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?
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() ?
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',
),
));
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)
Is this the 4th or 5th issue in this question?
Do we build the whole application this way?
Its an array, not an object
{{ $data_json['avatar_url'] }}
next?
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.