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;
    }
} 
 
		 
 
		 
 
		 
 
		