Access the API, then query

Hi,

I’m currently writing a php script to connect to my API and output certain information - (ultimately it will become a csv file)

Its working really well however I seem to be getting mixed results - if I look at the pro reports it shows there were 6 plugin updates, but if I access the API I am only getting 2 reported - so I’m guessing I’m showing the wrong API - does anyone know the correct api access?

I’m currently checking:

$site_non_mainwp_changes_url = $mainwp_api_url . 'site/non-mainwp-changes';
            $site_non_mainwp_changes_url .= '?site_id=' . $site_id;
            $site_non_mainwp_changes_url .= '&consumer_key=' . $api_key;
            $site_non_mainwp_changes_url .= '&consumer_secret=' . $api_secret;

            $ch_site_non_mainwp_changes = curl_init($site_non_mainwp_changes_url);
            curl_setopt($ch_site_non_mainwp_changes, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch_site_non_mainwp_changes, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer ' . $api_key
            ));

            $site_non_mainwp_changes_response = curl_exec($ch_site_non_mainwp_changes);

            if (curl_errno($ch_site_non_mainwp_changes)) {
                echo 'Curl error: ' . curl_error($ch_site_non_mainwp_changes);
            } else {
                $non_mainwp_changes = json_decode($site_non_mainwp_changes_response, true);

                // Count the number of plugin updates with action "updated" and context "plugins"
                $plugin_update_count = 0;
                foreach ($non_mainwp_changes as $pchange) {
                    if ($pchange['action'] === 'updated' && $pchange['context'] === 'plugins') {
                        $plugin_update_count++;
                    }
                }

                echo 'Plugin Updates: ' . $plugin_update_count . '<br>';

but its different to what pro reports says.

Any ideas if this is the right thing?

Hi Drew,

If you check the MainWP > Overview page, what do you see in the Updates Overview widget? Or, what do you see in the MainWP > Sites > Updates area?

How do you check for available updates in the Pro reports?

Hi @bogdan, Sorry , I should have been clearer - I’m talking about plugins that have happened. So on the care summary report I see that there were 21 wordfence security scans, 6 plugin updates, 1 theme update etc.

Its that information I’m pulling out.

Thanks

My full page code is:

// MainWP API URL
$mainwp_api_url = 'https://x.com/wp-json/mainwp/v1/';
$api_key = 'xx';
$api_secret = 'xx';

// Build the API URL to fetch all monitored sites
$api_url = $mainwp_api_url . 'sites/all-sites';
$api_url .= '?consumer_key=' . $api_key;
$api_url .= '&consumer_secret=' . $api_secret;

// Set up the cURL session
$ch = curl_init($api_url);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $api_key
));

// Execute the cURL request
$sites_all_sites_response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
    exit;
}

// Close cURL session
curl_close($ch);

// Check if the request was successful (HTTP status code 200)
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_status === 200) {
    // Parse the JSON response
    $monitored_sites = json_decode($sites_all_sites_response, true);

    // Iterate through the monitored sites
    foreach ($monitored_sites as $site) {
        $site_id = isset($site['id']) ? $site['id'] : 'N/A';
        $site_name = isset($site['name']) ? $site['name'] : 'N/A';
        $site_url = isset($site['url']) ? $site['url'] : 'N/A';

        // Make an API request to get site info, including the WordPress version
        $site_site_info_url = $mainwp_api_url . 'site/site-info';
        $site_site_info_url .= '?site_id=' . $site_id;
        $site_site_info_url .= '&consumer_key=' . $api_key;
        $site_site_info_url .= '&consumer_secret=' . $api_secret;

        $ch_site_site_info = curl_init($site_site_info_url);
        curl_setopt($ch_site_site_info, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch_site_site_info, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer ' . $api_key
        ));

        $site_site_info_response = curl_exec($ch_site_site_info);

        if (curl_errno($ch_site_site_info)) {
            echo 'Curl error: ' . curl_error($ch_site_site_info);
        } else {
            $site_site_info = json_decode($site_site_info_response, true);
            $wp_version = isset($site_site_info['wpversion']) ? $site_site_info['wpversion'] : 'N/A';
            $debug_mode = isset($site_site_info['debug_mode']) ? $site_site_info['debug_mode'] : 'N/A';
            $phpversion = isset($site_site_info['phpversion']) ? $site_site_info['phpversion'] : 'N/A';
            $child_version = isset($site_site_info['child_version']) ? $site_site_info['child_version'] : 'N/A';
            $memory_limit = isset($site_site_info['memory_limit']) ? $site_site_info['memory_limit'] : 'N/A';
            $mysql_version = isset($site_site_info['mysql_version']) ? $site_site_info['mysql_version'] : 'N/A';
            $db_size = isset($site_site_info['db_size']) ? $site_site_info['db_size'] : 'N/A';
            $themeactivated = isset($site_site_info['themeactivated']) ? $site_site_info['themeactivated'] : 'N/A';
        }

        // Make an API request to get plugin upgrades for the site
        $site_site_url = $mainwp_api_url . 'site/site';
        $site_site_url .= '?site_id=' . $site_id;
        $site_site_url .= '&consumer_key=' . $api_key;
        $site_site_url .= '&consumer_secret=' . $api_secret;

        $ch_site_site = curl_init($site_site_url);
        curl_setopt($ch_site_site, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch_site_site, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer ' . $api_key
        ));

        $site_site_response = curl_exec($ch_site_site);

        if (curl_errno($ch_site_site)) {
            echo 'Curl error: ' . curl_error($ch_site_site);
        } else {
            $site_site = json_decode($site_site_response, true);
            $plugin_upgrades = isset($site_site['plugin_upgrades']) ? $site_site['plugin_upgrades'] : 'N/A';
            echo '<hr><br>Site Name: ' . $site_name . '<br>';
            echo 'Site URL: ' . $site_url . '<br>';
            echo 'WordPress Version: ' . $wp_version . '<br>';
            echo 'Debug Mode: ' . $debug_mode . '<br>';
            echo 'PHP Version: ' . $phpversion . '<br>';
            echo 'Child Version: ' . $child_version . '<br>';
            echo 'Memory Limit: ' . $memory_limit . '<br>';
            echo 'MySQL Version: ' . $mysql_version . '<br>';
            echo 'DB Size: ' . $db_size . 'mb<br>';
            echo 'Theme Name: ' . $themeactivated . '<br>';

            // Make an API request to get non-MainWP changes for the site
            $site_non_mainwp_changes_url = $mainwp_api_url . 'site/non-mainwp-changes';
            $site_non_mainwp_changes_url .= '?site_id=' . $site_id;
            $site_non_mainwp_changes_url .= '&consumer_key=' . $api_key;
            $site_non_mainwp_changes_url .= '&consumer_secret=' . $api_secret;

            $ch_site_non_mainwp_changes = curl_init($site_non_mainwp_changes_url);
            curl_setopt($ch_site_non_mainwp_changes, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch_site_non_mainwp_changes, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer ' . $api_key
            ));

            $site_non_mainwp_changes_response = curl_exec($ch_site_non_mainwp_changes);

            if (curl_errno($ch_site_non_mainwp_changes)) {
                echo 'Curl error: ' . curl_error($ch_site_non_mainwp_changes);
            } else {
                $non_mainwp_changes = json_decode($site_non_mainwp_changes_response, true);
			
                // Create an array to store unique plugin updates
                $unique_plugin_updates = array();

                // Iterate through the changes and collect unique plugin updates
                foreach ($non_mainwp_changes as $change) {
                    if ($change['action'] === 'updated' && $change['context'] === 'plugins') {
                        $plugin_name = $change['name'];
                        $unique_plugin_updates[$plugin_name] = true;
                    }
                }

                // Count the number of unique plugin updates
                $plugin_update_count = count($unique_plugin_updates);

                // Output the count of plugin updates along with plugin names
                echo 'Plugin Updates Count: ' . $plugin_update_count . '<br>';
                echo 'Plugin Names: ' . implode(', ', array_keys($unique_plugin_updates)) . '<br>';
            }
        }
    }
} else {
    echo 'Failed to retrieve monitored sites. Status code: ' . $http_status;
}

This is showing me the site name, wp version etc etc, but doesnt show how many plugin updates were done. I’m not too worried about show a full list of from and to version numbers with dates, but I would like to see that 6 of them have been updated this month etc…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.