• Resolved demidarus

    (@demidarus)


    Hello πŸ‘‹
    I am currently implementing an API that checks for plugin updates in our wordpress instance.
    While trying to access the plugin-update API route(https://api.wordpress.org/plugins/update-check/1.1/), i always get a 200 response with a simple string as a body: “error”.

    Since the API documentation is without any information (it does not even specify what parameters this route needs), i tried using the code wordpress itself uses for the wp_update_plugins() function (https://developer.wordpress.org/reference/functions/wp_update_plugins/). This is what i currently have in my testing environment(local):

    $current->last_checked = time();
    set_site_transient('update_plugins', $current);
    $to_send = compact('pluginsForUpdates', 'active');
    $locales = array_values(get_available_languages());
    $locales = apply_filters('plugins_update_check_locales', $locales);
    $locales = array_unique($locales);
    $options = array(
    	'timeout'    => $timeout,
    	'body'       => array(
    		'plugins'      => wp_json_encode($to_send),
    		'translations' => wp_json_encode($translations),
    		'locale'       => wp_json_encode($locales),
    		'all'          => wp_json_encode(true),
    	),
    	'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url('/'),
    );
    $url      = 'http://api.wordpress.org/plugins/update-check/1.1/';
    $http_url = $url;
    $ssl      = wp_http_supports(array('ssl'));
    
    if ($ssl) {
    	$url = set_url_scheme($url, 'https');
    }
    
    $raw_response = wp_remote_post($url, $options);
    
    if ($ssl && is_wp_error($raw_response)) {
    	trigger_error(
    		sprintf(
    			/* translators: %s: Support forums URL. */
    			__('An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.'),
    			__('https://wordpress.org/support/forums/')
    		) . ' ' . __('(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)'),
    		headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
    	);
    	$raw_response = wp_remote_post($http_url, $options);
    }
    
    if (is_wp_error($raw_response) || 200 !== wp_remote_retrieve_response_code($raw_response)) {
    	return;
    }
    
    $raw_response = wp_remote_post($url, $options);
    $response = json_decode(wp_remote_retrieve_body($raw_response), true);
    
    if ($response && is_array($response)) {
    	$updates->response     = $response['plugins'];
    	$updates->translations = $response['translations'];
    	$updates->no_update    = $response['no_update'];
    }
    
    var_dump($updates);
    

    I still get a simple “error” response as a body.

    Thank you for your help.

    • This topic was modified 3 years, 7 months ago by demidarus.
    • This topic was modified 3 years, 7 months ago by demidarus.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Thread Starter demidarus

    (@demidarus)

    This is the HTTP Response that i get from the update plugins API:

    
    array (size=6)
      'headers' => 
        object(Requests_Utility_CaseInsensitiveDictionary)[1465]
          protected 'data' => 
            array (size=5)
              'server' => string 'nginx' (length=5)
              'date' => string 'Thu, 25 Aug 2022 15:19:35 GMT' (length=29)
              'content-type' => string 'text/plain; charset=utf-8' (length=25)
              'x-frame-options' => string 'SAMEORIGIN' (length=10)
              'x-nc' => string 'ord 7' (length=5)
      'body' => string 'error' (length=5)
      'response' => 
        array (size=2)
          'code' => int 200
          'message' => string 'OK' (length=2)
      'cookies' => 
        array (size=0)
          empty
      'filename' => null
      'http_response' => 
        object(WP_HTTP_Requests_Response)[1461]
          protected 'response' => 
            object(Requests_Response)[1462]
              public 'body' => string 'error' (length=5)
              public 'raw' => string 'HTTP/1.1 200 OK
    Server: nginx
    Date: Thu, 25 Aug 2022 15:19:35 GMT
    Content-Type: text/plain; charset=utf-8
    Transfer-Encoding: chunked
    Connection: close
    X-Frame-Options: SAMEORIGIN
    X-nc:  ord 7
    
    error' (length=207)
              public 'headers' => 
                object(Requests_Response_Headers)[1463]
                  ...
              public 'status_code' => int 200
              public 'protocol_version' => float 1.1
              public 'success' => boolean true
              public 'redirects' => int 0
              public 'url' => string 'https://api.wordpress.org/plugins/update-check/1.1/' (length=51)
              public 'history' => 
                array (size=0)
                  ...
              public 'cookies' => 
                object(Requests_Cookie_Jar)[1460]
                  ...
          protected 'filename' => null
          public 'data' => null
          public 'headers' => null
          public 'status' => null
    
    • This reply was modified 3 years, 7 months ago by demidarus.
Viewing 1 replies (of 1 total)

The topic ‘Error while trying to access WordPress API’ is closed to new replies.