XF 2.2 Extending IP ControllerPlugin

AndrewSimm

Well-known member
I've extended the controller plugin for IP to pull in additional IP information using an API. When I do a var_dump against $country, $city, etc, I get the expected values. My issue is these variables are not making their way to the template.
PHP:
<?php

namespace Andrew\ModeratorPanel\XF\ControllerPlugin;

use \XF\Mvc\Entity\Entity;

class Ip extends XFCP_Ip
{
    public function actionIp(Entity $content, array $breadcrumbs = [], $options = [])
    {

        $options = array_merge([
            'id' => 'ip_id',
            'view' => 'XF:Ip\Ip',
            'template' => 'content_ip_view',
            'extraViewParams' => []
        ], $options);

        $visitor = \XF::visitor();

        if (!$visitor->canViewIps())
        {
            return $this->error(\XF::phrase('no_ip_information_available'));
        }

        $ip = null;
        if (!empty($content[$options['id']]))
        {
            $ip = $this->em()->find('XF:Ip', $content[$options['id']]);
        }

        if (!$ip)
        {
            return $this->error(\XF::phrase('no_ip_information_available'));
        }

        // Call the parent action and store its response
        $parent = parent::actionIp($content, $breadcrumbs, $options);

        // Define your API access key
        $accessKey = '*******';

        // Using a dummy ip since testing on localhost
        $ipAddress = '134.201.250.155';

        // Construct the API URL
        $apiUrl = "http://api.ipstack.com/{$ipAddress}?access_key={$accessKey}";

        // Initialize cURL session
        $ch = curl_init($apiUrl);

        // Set cURL options
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Execute the cURL request and get the response
        $response = curl_exec($ch);

        // Check for cURL errors
        if (curl_errno($ch)) {
            // Handle cURL errors, for example, log them or return an error message
            return 'CURL Error: ' . curl_error($ch);
        } else {
            // Decode the JSON response
            $data = json_decode($response, true);

            // Check if the request was successful
            if ($data && isset($data['success']) && $data['success'] === false) {
                // Handle API errors, for example, log them or return an error message
                return 'API Error: ' . $data['error']['info'];
            } else {
                // Retrieve geolocation data
                $country = $data['country_name'];
                $region = $data['region_name'];
                $city = $data['city'];
                $zip = $data['zip'];
                $latitude = $data['latitude'];
                $longitude = $data['longitude'];

                // Set the geolocation data in the parent's viewParams
                $parent->setParams([
                    'country' => $country,
                    'region' => $region,
                    'city' => $city,
                    'zip' => $zip,
                    'latitude' => $latitude,
                    'longitude' => $longitude
                ]);
            }
        }

        // Close the cURL session
        curl_close($ch);

        // Return the parent
        return $parent;
    }
}
 
You should use \XF::app->http()->reader()->get() not raw curl API calls.

Additionally, the returning a string should instead be wrapped in $this->error('my error') as XF controller actions must return something derived from AbstractReply. You probably don't want to disable the ip form if the curl request fails as well.
 
You should use \XF::app->http()->reader()->get() not raw curl API calls.

Additionally, the returning a string should instead be wrapped in $this->error('my error') as XF controller actions must return something derived from AbstractReply. You probably don't want to disable the ip form if the curl request fails as well.
Good callouts. I will take a look at some example code any implement.

Any reason why my variables are not making it to the template? I validated that $country and $city have values from the dummy IP I used but they are not display in the template and the template modification is working fine.
 
Edit the style you are using, and use {{ dump($country) }} and see what it outputs.

Stock XenForo doesn't actually show you the output of all template modifications on the actual template you are actually using for the current style, which can be annoying to troubleshoot.

Alternatively, edit the compiled php file (for that template/style/language) and add var_dump($vars) to see what the template has actually received.
 
Back
Top Bottom