Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    Need help getting Json_decode obj PHP5

    Hello, I created a function which posts a link to a URL shortening API and returns the results in JSON format. Having trouble outputting the results.


    My func:
    PHP Code:
    function get_bitly_shorturl($longurl) {
        
    $url "http://api.bit.ly/shorten?version=2.0.1&login=myusernamer&apiKey=myapikey&format=json&history=1" "&longUrl=".urlencode($longurl);
     
    // in the above link i have removed my username and my api key for posting on this forum

        //using curl
        
    $curlObject curl_init();
        
    curl_setopt($curlObject,CURLOPT_URL,$url);
        
    curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
        
    curl_setopt($curlObject,CURLOPT_HEADER,false);
     
        
    $result_json curl_exec($curlObject);
        
    curl_close($curlObject);
     
        
    // decode JSON. Assumes that it is PHP5
        
    var_dump(json_decode($result_json));
        
    $resultJson json_decode($result_json);
        
        echo 
    $resultJson['results'][$longurl]['shortUrl'];
          return 
    $resultJson['results'][$longurl]['shortUrl']; // this line gives me the following error:  Fatal error: Cannot use object of type stdClass as array in /home/myscript/path/myscript.php on line 24

    the var_dump is:

    Code:
    object(stdClass)#1 (4) { ["errorCode"]=>  int(0) ["errorMessage"]=>  string(0) "" ["results"]=>  object(stdClass)#2 (1) { ["http://www.mylongurl.com"]=>  object(stdClass)#3 (4) { ["hash"]=>  string(5) "blajadsdf324" ["shortKeywordUrl"]=>  string(0) "" ["shortUrl"]=>  string(20) "http://bit.ly/dsd4wscY" ["userHash"]=>  string(6) "dsd4wscY" } } ["statusCode"]=>  string(2) "OK" }
    I am unable to echo or return the shortUrl in my $resultJson object. Can someone help?

    return $resultJson['results'][$longurl]['shortUrl']; appears to give the fatal error

    I have tried a number of different methods, including: return $resultJson['results']['shortUrl'];

    same error

    please help

  • #2
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    can anyone tell me why this doesn't work?

    please?

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by Bobafart View Post
    can anyone tell me why this doesn't work?

    please?
    Code:
    object(stdClass)#1 (4) { 
    	["errorCode"]=>  int(0) 
    	["errorMessage"]=>  string(0) "" 
    	["results"]=>  object(stdClass)#2 (1) {
    		["http://www.mylongurl.com"]=>  object(stdClass)#3 (4) {
    		 	 ["hash"]=>  string(5) "blajadsdf324"
    			 ["shortKeywordUrl"]=>  string(0) "" 
    			 ["shortUrl"]=>  string(20) "http://bit.ly/dsd4wscY" 
    			 ["userHash"]=>  string(6) "dsd4wscY" 
    		 } 
    	} 
    	["statusCode"]=>  string(2) "OK" 
    }
    Use <pre> tags around a var_dump presents its easier and make easier for others to help you
    So to get your Val you want to do
    PHP Code:
    $var json_decode();
    $add $var->results->http://www.mylongurl.com->shortUrl; 
    But that wont work due to the // so sorry i cant be much help but that's how JSON normally works
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)


  •  

    Posting Permissions

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