How can I loop through a JSON Object in PHP? - php

I have a variable $arr in PHP. It has the following data inside it.
This is my PHP Code.
$data = array(
'Request' => 'StockStatus',
'merchant_id' => 'shipm8',
'hash' => '09335f393d4155d9334ed61385712999'
);
$url = 'https://ship2you.com/ship2you/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$arr = json_decode($result, true);
foreach ($arr as $value) {
echo $value['packagename'];
}
I want to loop through it. How can I achieve it? I tried using foreach but it gives me error. Thanks in advance.

You have to decode your CURL output string twice:-
$arr = json_decode(json_decode($result, true),true);
foreach ($arr as $value) {
echo "<pre/>";print_r($value['packagename']);
}
Note:- #Xatenev mentioned the correct thing:-
The json has escaped quotes. When a json with escaped quotes is passed to json_decode() it only removes all the escaped sequences. When calling json_decode() again, it decodes it correctly

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$arr = json_decode($result);
foreach ($arr as $arr_item) {
echo $arr_item->user_id;
}

json_decode($json, true);
If the second parameter is true, it will return array. In case it is not what you are looking for, please show the code you have.

Related

Multiple cURL data been overwritten on PHP for each

I need to call cURL multiple times to get different JSON responses so I followed the code in the answer of this question:
Multiple curl json and json print.
Now I realize that the variable that stores the return data is only holding the information from the last URL in the array (it's been overwritten). Here is my code:
$urls = Array(
'https://example.com/projects/277199/roles.json',
'https://example.com/projects/292291/roles.json'
);
foreach ($urls as $key=>$url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "XXX:YYY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ch_response = curl_exec($ch);
curl_close($ch);
$rolesData = json_decode($ch_response,true);
}
print_r($rolesData); //It's only printing the data from the last element in the urls array
How do I correctly store the data?
just add [] or [$key] to $rolesData
foreach ($urls as $key=>$url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "XXX:YYY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ch_response = curl_exec($ch);
curl_close($ch);
$rolesData[] = json_decode($ch_response,true);
}
print_r($rolesData);

Using cURL in a foreach-loop (php)

I have many HTTPS-URLS where I have to find a Special Phrase, so I´m trying to use cURL in a foreach Loop, but it´s not working.
...
foreach($sites as $site) {
$URL = $site;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
}
print substr($response, $start, $i);
...
If I use just a single HTTPS-URL I get the Phrase, but inside a foreach-loop it´s not working.
Can someone help me? (:
Please excuse my poor english..
this may help :) store result inside an array
$sites = ['https://stackoverflow.com','http://example.org'];
$result = [];
foreach ($sites as $site) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $site);
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Curl");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// execute the given URL, and return output
$result[] = curl_exec($ch);
curl_close($ch);
}
var_dump($result);

array_merge(): Argument #1 is not an array

need your help on this... got an error on my array_merge
here's my code:
//first
$url1="https://www.zopim.com/api/v2/chats";
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url1);
curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$output1 = curl_exec($ch1);
$info1 = curl_getinfo($ch1);
curl_close($ch1);
$chats1 = json_decode($output1,true);
//second
$url2="https://www.zopim.com/api/v2/chats?page=2";
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url2);
curl_setopt($ch2, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$output2 = curl_exec($ch2);
$info2 = curl_getinfo($ch2);
curl_close($ch2);
$chats2 = json_decode($output2,true);
$r = [];
if(is_array($chats1) && is_array($chats2))
{
foreach($chats1 as $key => $array)
{
$r[$key] = array_merge($chats2[$key], $array);
}
}
else
{
echo 'problem with json';
}
echo json_encode($r, JSON_UNESCAPED_SLASHES);
I need to combine a two json using array_merge()... im using curl authorization to call my API..
but when i try to run the code it has a error:
here's number 44 error:
This means your json_decode fails. It will fail if the string is no valid JSON. When it fails json_decode returns either null or false, so you have to check if the response is valid:
$chats1 = json_decode($output1, true);
$chats2 = json_decode($output2, true);
if ($chats1 && $chats2 && is_array($chats1) && is_array($chats2)) {
// your code goes here
}

Multiple curl json and json print

I want to access multiples urls via curl and print the son output. I've seen this: multiple cURL and output JSON? but I`am not able make it work anyway...
my code:
<?php
$urls = Array(
'URLtoJSON1',
'URLtoJSON1'
);
for($i = 0; $i < 3; $i++) {
$curl[$i] = curl_init($urls);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "YYY:XXX");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response[$i] = curl_exec($curl);
curl_close($curl);
$data = json_decode($curl_response[$i]);
$name[$i] = $data->fullDisplayName;
$datum[$i] = $data->timestamp;
$result[$i] = $data->result;
}
// here I`d love to be able echo output $name[URLtoJSON], etc...
?>
thank you for any help.
Instead of doing a for loop, you can make a foreach loop that iterates over your $urls array by doing foreach ($urls as $key=>$url). $key will hold the index of the array (starting at 0) and $url will hold the URL.
Here is what the resulting code would look like:
$urls = Array(
'URLtoJSON1',
'URLtoJSON2'
);
foreach ($urls as $key=>$url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "YYY:XXX");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ch_post_data);
$ch_response = curl_exec($ch);
curl_close($ch);
$data = json_decode($ch_response);
$name[$key] = $data->fullDisplayName;
$datum[$key] = $data->timestamp;
$result[$key] = $data->result;
}
Now if you want to access $name of the first URL, you would just do $echo $name[0];
You can also access $datum or $result in a similar way.

format curl results in php

Below I have a piece of code to return some information through an API.
$page = 'http://api.duedil.com/sandbox/v2/company/03977902.json? fields=get_all&api_key=***********';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
This prints out the following results http://www.visitrack.co.uk/testdata.php
what i am trying to do is split the results in to individual variables.
Thanks
Something like this should do the trick:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // forces curl_exec() to return page as a string
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, TRUE); // parse json string to array
print_r($data);

Categories