Zugriff auf Objekt

Ich komme mal wieder bei einem JSON Objekt nicht weiter - evtl. kann mir jemand helfen.

Via JSON Decode bekomme ich den folgenden Eintrag bei einer DNS Abfrage:

Array
(
[0] => Array
(
[data] => 91.57.80.249
[name] => sub.domain.net
[ttl] => 3600
[type] => A
)

)

Ich will gerne auf „data“ zugreifen.

Ich habe schon alles probiert um auf diesen Array zuzugreifen, aber ich bekomme es nicht hin. Ich bleibe immer bei [0] hängen.

$array_name[0][data]

Probiere es mal mit [0][’data’]

Stimmt, Hochkomma fehlen bei mir.

Schon mal Danke für die schnellen Antworten

Puh … das hatte ich auch probiert


    $dn = json_decode($result, true);
 
    //echo '<pre>';
    print_r($dn);
    //echo '</pre>';
	
	$dnsip = [0][’data’];
	print_r($dn);


Da bekomme ich leider

Warning: Use of undefined constant ’data’ - assumed ‚’data’‘ (this will throw an Error in a future version of PHP) in /mnt/data/symcon/scripts/14466.ips.php on line 67

ich habe auch [0][1] oder „data“ probiert oder ohne " oder ’ … entweder kommt der komplette Array zurück oder ein Fehler

Das hier ist nicht identisch!
Du nutzt das falsche Hochkomma.


$dnsip = $dn[0]['data']; // richtig mit '
$dnsip = $dn[0][’data’]; // falsch mit ’

Michael

Danke … jetzt nutze ich

$dnsip = [0][‚data‘];

mit Resultat

Notice: Undefined index: data in /mnt/data/symcon/scripts/14466.ips.php on line 67

Das habe ich als Basis



Array
(
    [0] => Array
        (
            [data] => 91.57.80.249
            [name] => home.lueckel.net
            [ttl] => 3600
            [type] => A
        )

)

Zuweisen von was ?
Von nix geht nicht.
Fehlt da nicht ein $dn :wink:
Michael

Hi Michael,

da hast Du natürlich recht - ich hatte $dn vergessen.

Also jetzt …
$dnsip = $dn[0][‚data‘];

Beim print_r kommt aber wieder der komplette Array und nicht die IP Adresse von Data?

Grüße

Enno

Zeig dein Code.
Michael

Ohne den Code genau zu kennen wuerde ich sagen vergiss das Print_r.
Probier echo $dnsip.

Damit will ich bein DynDNS bei GoDaddy erreichen … bis auf die aktuell gesetzt IP auszulesen, klappt alles.

Die Sektion die nicht geht steht in „Return current setting“


<?


$key = ""; #key for godaddy developer API
$secret = ""; #Secret for godday developer API
$domain = ".net";
$aname = "..net";

//Current IP
/*
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://ipinfo.io/json');
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$json = curl_exec($ch);
curl_close($ch);

//echo $json;

$data = json_decode($json);


$ip = $data->ip;
echo $ip;
*/

//Return current setting 
     
    $url = 'https://api.godaddy.com/v1/domains/'.$domain.'/records/A/'.$aname;
	
    
    // set your key and secret
    $header = array(
        'Authorization: sso-key '.$key.':'.$secret
    );
	//print_r ($header);
 
    //open connection
    $ch = curl_init();
    $timeout=60;
 
    //set the url and other options for curl
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);  
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE 
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $variable);
    //curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 
    //execute call and return response data.
    $result = curl_exec($ch);
 
    //close curl connection
    curl_close($ch);
 
    // decode the json response
    $dn = json_decode($result, true);
 
    //echo '<pre>';
    print_r($dn);
    //echo '</pre>';
	
	$dnsip = $dn[0]['data'];
	print_r($dn);


// Update

/*
    $input_json = array("data" => $ip, "ttl"=> 3600);
	//print_r ($input_json);
	$data_string = json_encode($input_json);
	$data_string = "[ ".$data_string." ]";
	print_r ($data_string);
	
	//$url = 'https://api.godaddy.com/v1/domains/lueckel.net/records/A/home.lueckel.net';
	$url = 'https://api.godaddy.com/v1/domains/'.$domain.'/records/A/'.$aname;
    
    // set your key and secret
    $header = array(
        'Authorization: sso-key '.$key.':'.$secret,
		'Content-Type: application/json',
		'Accept: application/json'  		
    );
	//print_r ($header);
 
    //open connection
    $ch = curl_init();
    $timeout=60;
 
    //set the url and other options for curl
    curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);  
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json-patch');                                                                              
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $input_json);
    curl_setopt($ch, CURLOPT_POST, true);
    
 
    //execute call and return response data.
	
    $result = curl_exec($ch);
 
    //close curl connection
    curl_close($ch);
 
    // decode the json response
    $dn = json_decode($result, true);
 
    echo '<pre>';
    print_r($dn);
    echo '</pre>';

*/

?>


Ahhhhhh… Du gibst das falsche aus:
$dnsip = $dn[0][‚data‘];

print_r($dn); // da muss $dnsip rein!
Michael

Sagen wir es mal so … ich bin jetzt gerade rot geworden und bin die nächsten Stunden im Keller und schäme mich.

Danke Michael - wie immer