License API

Cupara

Well-known member
Does anyone know what the returned data looks like after decode?

Example:
Code:
Data:
{
license_token => 'blah',
is_valid => 'yes'
}
 
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.
 
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.
 
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:
Error: call to URL http://xenforo.com/api/license-lookup.json failed with status 200, response {"error":["Please enter a validation token."],"templateHtml":"\n\n
\n\t<\/a>\n\t\n\t\t
The following error occurred<\/h2>\n\t\t\n\t\t

\n\t\t\n\t\t\tPlease enter a validation token.<\/label>\n\t\t\n\t\t<\/div>\n\t\n<\/div>"}, curl_error , curl_errno 0

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.
 
I looked at the helper for facebook, I'm going that route, I'll report back if all is good or what.
 
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.
 
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.
 
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.
 
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>"
}
 
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.
 
Yeah see I can't see that when running a cron for some reason.
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.


or I'm just constructing this all wrong.

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.webp

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`");
 
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
 
Top Bottom