1

I have the following array of json data. When I try to loop using a for each, php gives me the error "Object of class stdClass could not be converted to string". I want to access the nameserver in the authns array, and the soa array. How do I go about doing that?

stdClass Object ( 
    [error] => 
    [domain] => whoisdoma.net 
    [ip_address] => 108.162.199.120   
    [authns] => stdClass Object ( 
        [nameserver] => uma.ns.cloudflare.com 
        [ip] => 173.245.58.146 
        [location] => San Francisco 
    ) 
    [soa] => stdClass Object ( 
        [nameserver] => tom.ns.cloudflare.com 
        [email] => dns.cloudflare.com 
        [serial] => 2015505396 
        [refresh] => 10000 
        [retry] => 2400 
        [expiry] => 604800 
        [minimum] => 3600 
    )
) 

This is the code I'm using to get the data:

 <?php
 $domain = $_GET['domain'];
//set the url
$url = "http://api.site.ga/v1/dns/lookup?domain=".$domain;

//start a curl session
$ch = curl_init();
$timeout = 5;

//set curl options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

//get the data
$data = curl_exec($ch);

//decode the data
$jsonData = json_decode($data);

//close the curl session
curl_close($ch);

?>

This is the for each loop I'm using

<?php 

foreach($jsonData->authns as $authns)
{
    echo $authns->nameserver;
}

?>
6
  • First things first. Have you converted it from json using json_decode()? If so try: $object->authns->nameserver. In any case, what is it exactly that you're doing which throws that error Commented Jun 22, 2014 at 15:57
  • Yes I have, just added the code I'm using to do that Commented Jun 22, 2014 at 16:00
  • so when is the error thrown? Commented Jun 22, 2014 at 16:01
  • its thrown at the for each loop when I use the authns variable Commented Jun 22, 2014 at 16:04
  • Can you do a full var_dump of $jsonData and post it in a pastebin? Commented Jun 22, 2014 at 16:08

2 Answers 2

2

You need to decode the json object using an associative array.

Here is an exmaple:

$data = json_decode(file_get_contents('http://api.domain.com/call'), true);

$data is now an associative array.

Sign up to request clarification or add additional context in comments.

3 Comments

He didn't send the 'true' parameter to json_decode(); That is why he can't access elements in the array.
Now I see "Illegal string offset 'nameserver'", the code for that is "<?php foreach($jsonData['authns'] as $nsData): ?> <tr> <td><?php echo $nsData['nameserver']; ?></td> </tr> <?php endforeach; ?>"
Do a print_r($data); so that you see how the array is being structured.
1

You are looping through authns while it is an object by itself. So your nameserver which you want is actually $authns in its first iteration.

Just access it like $jsonData->authns->nameserver

try this for yourself

foreach($jsonData->authns as $authns)
{
    echo $authns;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.