1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

License API

Discussion in 'XenForo Development Discussions' started by Mythotical, May 15, 2013.

  1. Mythotical

    Mythotical Well-Known Member

    Does anyone know what the returned data looks like after decode?

    Example:
    Code:
    Data:
    {
    license_token => 'blah',
    is_valid => 'yes'
    }
    
     
  2. digitalpoint

    digitalpoint Well-Known Member

  3. Mythotical

    Mythotical Well-Known Member

    No I read that, what I mean is how it is returned in format, not the data itself.
     
  4. Mike

    Mike XenForo Developer Staff Member

    It's JSON.
     
  5. Mythotical

    Mythotical Well-Known Member

    I know that too. With JSON responses are given like such:
    Code:
    data:
    array {
    foo: bar,
    bar: foo
    array {
              foo: bar,
              bar:foo
              }
    }
    
    The problem is without knowing that it is hard to determine if I should be using $data->is_valid or $data[0]->is_valid or $data[1]->is_valid

    The dilemma I'm facing is that at this point I have it running as a cron but I'm not even sure my JSON request is successful.

    Going against what I wanted to do but I need to get this solved so I can start my giveaway this weekend. Anyway as you can see I'm using cURL but no server errors or anything. Cron is successful but that is it, doesn't update the xf_user table or anything.
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    First of all, I'd highly recommend using the XenForo HTTP helper. There's a great example of how this works in XenForo_Helper_Facebook

    It's a lot simpler to set up for a start.

    Aside from that, first place to start is to try and debug the $response. As nothing is updated in the database, I would hazard a guess that there's no $response so just below your $response and before the if statement you should use:

    Zend_Debug::dump($response);

    That should dump the contents of $response at the top of your screen.
     
  7. Mythotical

    Mythotical Well-Known Member

    Ok so I have an error that I forgot about, even though I'm supplying the token of my own use which I don't mind posting above as I will change it once testing is done, I get this error:
    Chris, I will look at the helper, hopefully that will be easier but JSON is not my thing, new to me and I'm still learning.
     
  8. Mythotical

    Mythotical Well-Known Member

    I looked at the helper for facebook, I'm going that route, I'll report back if all is good or what.
     
  9. Mythotical

    Mythotical Well-Known Member

    Ok so far I managed to get it throw some errors, I tried doing $response->license_token but it did not recognize that so I'm not sure if I can use XenForo_Helper_Http for this or make my own.
     
  10. Chris D

    Chris D XenForo Developer Staff Member

    As per the Facebook helper you need to use

    $body = $response->getBody(); ($response contains the entire response including headers, your actual data is in the body)

    Then you want to do $decoded = json_decode($body, true); (True casts it out to an associative array rather than an object)

    $decoded['license_token'] should then contain your license token.
     
  11. Mythotical

    Mythotical Well-Known Member

    Yep I have that but I'll double check to make sure I have all necessary data.
     
  12. Mythotical

    Mythotical Well-Known Member

    EDIT: Unfortunately code is not available to share and plan to release an addon.
     
  13. Chris D

    Chris D XenForo Developer Staff Member

    Why are you using GET?

    Capture.PNG
     
  14. Mythotical

    Mythotical Well-Known Member

    OMG seriously, how did I miss that? Last night I had that as POST so I think I may have undid my change before going to bed. Anyway it is all good now, thanks Chris for all the help.
     
  15. Chris D

    Chris D XenForo Developer Staff Member

    No problem.

    This is the output of your original code. You don't need to do this blind with your finger in the air. If something isn't working, even if there's no error, there's usually going to be some way of analyzing what data has been returned:

    Code:
    array(2) {
      ["error"] => array(1) {
        [0] => string(83) "This action is available via POST only. Please press the back button and try again."
      }
      ["templateHtml"] => string(312) "
    
    <div class="errorOverlay">
    	<a class="close OverlayCloser"></a>
    	
    		<h2 class="heading">The following error occurred</h2>
    		
    		<div class="baseHtml">
    		
    			<label for="ctrl_0" class="OverlayCloser">This action is available via POST only. Please press the back button and try again.</label>
    		
    		</div>
    	
    </div>"
    }
     
  16. Mythotical

    Mythotical Well-Known Member

    Yeah see I can't see that when running a cron for some reason.

    Ok I'm stuck on another issue, I'm trying to run foreach to process all users that have token_valid field as 0.

    PHP:
        public static function license()
        {
            
    $options XenForo_Application::get('options');
            
    $db XenForo_Application::get('db');

                
    $client XenForo_Helper_Http::getClient('http://xenforo.com/api/license-lookup.json');

                
    $users $db->query("SELECT * FROM `xf_user` WHERE token_valid = 0");
                
                foreach (
    $users AS $user)
                {
                    
    $client->setParameterGet('token'$user['token'], 'domain'$user['domain']);

                    
    $response $client->request('POST');

                    
    $new json_decode($response->getBody(), true);

                    if (
    $new)
                    {
                            
    $db->update('xf_user',
                                array(
    'token' => $new['license_token'], 'token_valid' => '1'),
                                
    'user_id = 1'
                            
    );
                    }
                }

        }
    I'm not sure but apparently it won't grab the user fields or I'm just constructing this all wrong.
     
  17. Chris D

    Chris D XenForo Developer Staff Member

    There's ways around that but quite simply you could just set up a new controller action somewhere so you can access the URL and run the code on demand. It will then render the output you might need to see for debugging purposes.


    Yep. Again, let's see what's held in $users... bearing in mind I don't have a token_valid column so I'll just get all my test users:

    PHP:
    $users $db->query("SELECT * FROM `xf_user`");
    query.PNG

    I already knew, but that tells me straight away, $users isn't holding what you think it is. It's holding the raw MySqli database statement object.

    What you need to do is:

    PHP:
    $users $db->fetchAll("SELECT * FROM `xf_user`");
     
  18. Mythotical

    Mythotical Well-Known Member

    Ok I updated it and this is what I'm getting:
    Same code as I put above except I changed query to fetchAll.
     
  19. Chris D

    Chris D XenForo Developer Staff Member

    First of all $new will always contain something so doing if ($new) will always evaluate as true. That's because the response will always contain some data even if it's an error.

    So if one of your users doesn't have a domain or token then the response back from the API will be an error so if ($new) will evaluate true and that database update query will try to run.

    So, with that in mind, this should work better:

    PHP:
                    $options XenForo_Application::get('options');
                    
    $db XenForo_Application::get('db');

                    
    $client XenForo_Helper_Http::getClient('http://xenforo.com/api/license-lookup.json');

                    
    $users $db->fetchAll("SELECT * FROM `xf_user`");
                    
                    foreach (
    $users AS $user)
                    {
                        
    $client->setParameterPost('token'$user['token'], 'domain'$user['domain']);

                        
    $response $client->request('POST');

                        
    $new json_decode($response->getBody(), true);

                        if (!empty(
    $new['license_token']))
                        {
                                
    $db->update('xf_user',
                                    array(
    'token' => $new['license_token'], 'token_valid' => '1'),
                                    
    'user_id = 1'
                                
    );
                        }
                    }
    Oh notice as well I've used $client->setParameterPost whereas you were still using $client->setParameterGet
     
  20. Mythotical

    Mythotical Well-Known Member

    Perfect, thank you. Oh oops, should have changed that too. Check for a PC from me.
     

Share This Page