Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    376

    json_decode restituisce NULL o errore

    ciao a tutti,
    devo prendere, singolarmente, gli elementi restituiti da questo sito

    Questo codice restituisce "Trying to get property 'as_country_code' of non-object.."
    codice:
    $org = "https://api.iptoasn.com/v1/as/ip/$ip";
    $character = json_decode($org);
    echo $character->as_country_code;

    questo invece NULL:
    codice:
    $org = "https://api.iptoasn.com/v1/as/ip/$ip";
    $array = json_decode($org, true);
    var_dump($array);

  2. #2
    Utente di HTML.it L'avatar di boots
    Registrato dal
    Oct 2012
    Messaggi
    1,626
    così stai cercando di decodificare la stringa "https://api.iptoasn.com/v1/as/ip/8.8.8.8" ... che non è un json
    Prima recupera il json e poi lo passi alla json_decode

    Codice PHP:
    $org file_get_contents("https://api.iptoasn.com/v1/as/ip/$ip");
    $character json_decode($org);
    echo 
    $character->as_country_code

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    376
    così restituisce:

    codice:
    Warning:   file_get_contents(https://api.iptoasn.com/v1/as/ip/8.8.8.8): failed  to open stream: HTTP request failed! HTTP/1.0 403 Forbidden  in...

  4. #4
    Utente di HTML.it L'avatar di boots
    Registrato dal
    Oct 2012
    Messaggi
    1,626
    Ho provato lo script sulla mia macchina e funziona.
    Come vedi il server remoto ti blocca la richiesta. Dalla macchina in cui gira php, riesci a "vedere" il link ?
    Potresti provare a fare una richiesta non da php . Es da shell a dare un wget https://api.iptoasn.com/v1/as/ip/8.8.8.8 e vedere se va.

    Se riesci ad accedere vuol dire che la file_get_contents usa qualche header che ti fa bloccare la richiesta. A questo punto potresti provare a modificare l'header (puoi passare un context alla funzione) oppure prova ad usare cURL al posto della file_get_contents

  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    376
    leggo però che altervista blocca la funzione file_get_contents. Consigliano di usare fsokopen invece.
    Ma anche in questo caso, l'errore è:

    Warning: json_decode() expects parameter 1 to be string, resource given in

    Notice: Trying to get property 'as_country_code' of non-object in

  6. #6
    Utente di HTML.it L'avatar di boots
    Registrato dal
    Oct 2012
    Messaggi
    1,626
    posta quello che hai fatto. Stai passando null alla decode, ovvio che poi ti da errore

  7. #7
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    376
    eccomi

    $org = fsockopen("https://api.iptoasn.com/v1/as/ip/$ip");
    $character = json_decode($org);
    echo $character->as_country_code;

  8. #8
    Utente di HTML.it L'avatar di boots
    Registrato dal
    Oct 2012
    Messaggi
    1,626
    Fosse così semplice...
    Prova così:
    Codice PHP:
    $host 'api.iptoasn.com';
    $uri  '/v1/as/ip/8.8.8.8';
    $response "";
    if (
    $fp fsockopen('ssl://'$host443$errno$errstr30)) {
       
    $msg  'GET '.$uri.' HTTP/1.1' "\r\n";
       
    $msg .= 'Host: ' $host "\r\n";
       
    $msg .= 'Connection: close' "\r\n\r\n";
       if ( 
    fwrite($fp$msg) ) {
          while ( !
    feof($fp) ) {
             
    $response .= fgets($fp1024);
          }
       }
       
    fclose($fp);
    } else {
       
    $response false;
    }
    echo 
    $response
    $response contiene non solo il json ma anche l'header, quindi non puoi darlo direttamente alla decode. Però prima vediamo se ti con la fsockopen riesci ad ottenere qualcosa. (Il codice sopra l'ho testato e funziona)

  9. #9
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    376
    sì adesso mi stampa i valori del sito remoto in una stringa!

  10. #10
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    376
    adesso posso già usare il json o non ancora?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.