[RESOLVED] Problems setting vars based on json result
Results 1 to 4 of 4

Thread: [RESOLVED] Problems setting vars based on json result

  1. #1
    Junior Member
    Join Date
    Oct 2016
    Posts
    4

    resolved [RESOLVED] Problems setting vars based on json result

    Hi there everyone!

    I'm having two problems with some code I've written. First, it's printing the json info to screen although I don't want it to and secondly, I'm having problems retrieving the json info for setting variables.

    PHP Code:
    $ipurl "http://ipinfo.io/".$ip;

    $cURL curl_init();

    curl_setopt($cURLCURLOPT_URL$ipurl);
    curl_setopt($cURLCURLOPT_HTTPGETtrue);

    curl_setopt($cURLCURLOPT_HTTPHEADER, array(
        
    'Content-Type: application/json',
        
    'Accept: application/json'
    ));

    $result curl_exec($cURL);
    $json json_decode($resulttrue);
    curl_close($cURL);


    $hostname $json[0]['hostname'];
    $city $json['city'];
    $region $json['region'];
    $country $json['country'];
    $loc $json['loc'];
    $org $json['org'];
    $postcode $json['postcode']; 
    So, it seems

    PHP Code:
    $result curl_exec($cURL); 
    is printing the json info to screen. How do I stop this?

    and secondly, although I've used json_decode and have tried to set variables but although it prints to screen, trying to use $json['elementname'] leaves me with an empty variable. What do I need to do to set these?

    Thanks for your time!

  2. #2
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,152
    Regaring it returns to screen, you need to use the option CURLOPT_RETURNTRANSFER and set it to true as per the documentation Here.

    as for getting the jason, one you'll want to validate the the curl call actually completed successfully (probably using curl_info to get the return code), as well you aren't getting a json string in $result because of the return transfer.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  3. #3
    Junior Member
    Join Date
    Oct 2016
    Posts
    4
    Thank you very much for your help, Derokorian, it seems to be working fantastically now!

  4. #4
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,152
    Quote Originally Posted by schwim2 View Post
    Thank you very much for your help, Derokorian, it seems to be working fantastically now!
    No problem, glad it worked. Now to just memorize the plethora of CURLOPT_* constants and how to use them :P (no really though, I just referenced my existing code, and linked in case you needed more constants hehe)
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •