• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress a2z

WordPress a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Themes
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / Akismet::http_post() – Make a POST request to the Akismet API.

You appear to be a bot. Output may be restricted

Description

Make a POST request to the Akismet API.

Usage

$array = Akismet::http_post( $request, $path, $ip );

Parameters

$request
( string ) required – The body of the request.
$path
( string ) required – The path for the request.
$ip
( string ) optional – The specific IP address to hit.

Returns

array A two-member array consisting of the headers and the response body, both empty in the case of a failure.

Source

File name: akismet/class.akismet.php
Lines:

1 to 100 of 103
  public static function http_post( $request, $path, $ip=null ) {

    $akismet_ua = sprintf( 'WordPress/%s | Akismet/%s', $GLOBALS['wp_version'], constant( 'AKISMET_VERSION' ) );
    $akismet_ua = apply_filters( 'akismet_ua', $akismet_ua );

    $content_length = strlen( $request );

    $api_key   = self::get_api_key();
    $host      = self::API_HOST;

    if ( !empty( $api_key ) )
      $host = $api_key.'.'.$host;

    $http_host = $host;
    // use a specific IP if provided
    // needed by Akismet_Admin::check_server_connectivity()
    if ( $ip && long2ip( ip2long( $ip ) ) ) {
      $http_host = $ip;
    }

    $http_args = array(
      'body' => $request,
      'headers' => array(
        'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),
        'Host' => $host,
        'User-Agent' => $akismet_ua,
      ),
      'httpversion' => '1.0',
      'timeout' => 15
    );

    $akismet_url = $http_akismet_url = "http://{$http_host}/1.1/{$path}";

    
/**
 * Try SSL first; if that fails, try without it and don't try it again for a while.
 */

    $ssl = $ssl_failed = false;

    // Check if SSL requests were disabled fewer than X hours ago.
    $ssl_disabled = get_option( 'akismet_ssl_disabled' );

    if ( $ssl_disabled && $ssl_disabled < ( time() - 60 * 60 * 24 ) ) { // 24 hours
      $ssl_disabled = false;
      delete_option( 'akismet_ssl_disabled' );
    }
    else if ( $ssl_disabled ) {
      do_action( 'akismet_ssl_disabled' );
    }

    if ( ! $ssl_disabled && ( $ssl = wp_http_supports( array( 'ssl' ) ) ) ) {
      $akismet_url = set_url_scheme( $akismet_url, 'https' );

      do_action( 'akismet_https_request_pre' );
    }

    $response = wp_remote_post( $akismet_url, $http_args );

    Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) );

    if ( $ssl && is_wp_error( $response ) ) {
      do_action( 'akismet_https_request_failure', $response );

      // Intermittent connection problems may cause the first HTTPS
      // request to fail and subsequent HTTP requests to succeed randomly.
      // Retry the HTTPS request once before disabling SSL for a time.
      $response = wp_remote_post( $akismet_url, $http_args );
      
      Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) );

      if ( is_wp_error( $response ) ) {
        $ssl_failed = true;

        do_action( 'akismet_https_request_failure', $response );

        do_action( 'akismet_http_request_pre' );

        // Try the request again without SSL.
        $response = wp_remote_post( $http_akismet_url, $http_args );

        Akismet::log( compact( 'http_akismet_url', 'http_args', 'response' ) );
      }
    }

    if ( is_wp_error( $response ) ) {
      do_action( 'akismet_request_failure', $response );

      return array( '', '' );
    }

    if ( $ssl_failed ) {
      // The request failed when using SSL but succeeded without it. Disable SSL for future requests.
      update_option( 'akismet_ssl_disabled', time() );
      
      do_action( 'akismet_https_disabled' );
    }
    
    $simplified_response = array( $response['headers'], $response['body'] );
    
    self::update_alert( $simplified_response );
 
[1] [2] Next »

 View on GitHub View on Trac

Published: 19th December 2019 | Last updated: 19th December 2019

Primary Sidebar

Information

Function name: Akismet::http_post
Class ref: Akismet
Plugin ref: Akismet
Version: 5.0.1
Sourcefile: class.akismet.php
File ref: class.akismet.php
Deprecated?: No
API Letters: A,H,P

Footer

WordPress a2z
WordPress a2z
WordPress Dynamic API Reference
WordPress 6.1.1. PHP: 8.0.28
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  wp-a2z.org
© Copyright WordPress a2z 2014-2023. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites