Skip to content

Instantly share code, notes, and snippets.

@carlalexander
Last active August 13, 2021 00:40
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save carlalexander/c779b473f62dcd1a4ca26fcaa637ec59 to your computer and use it in GitHub Desktop.
Save carlalexander/c779b473f62dcd1a4ca26fcaa637ec59 to your computer and use it in GitHub Desktop.
WordPress "Expect" header fix
<?php
/**
* By default, cURL sends the "Expect" header all the time which severely impacts
* performance. Instead, we'll send it if the body is larger than 1 mb like
* Guzzle does.
*/
function add_expect_header(array $arguments)
{
$arguments['headers']['expect'] = '';
if (is_array($arguments['body'])) {
$bytesize = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arguments['body']));
foreach ($iterator as $datum) {
$bytesize += strlen((string) $datum);
if ($bytesize >= 1048576) {
$arguments['headers']['expect'] = '100-Continue';
break;
}
}
} elseif (!empty($arguments['body']) && strlen((string) $arguments['body']) > 1048576) {
$arguments['headers']['expect'] = '100-Continue';
}
return $arguments;
}
add_filter('http_request_args', 'add_expect_header');
@Lolosan
Copy link

Lolosan commented Aug 4, 2021

It works great, but i still get "Array to string conversion", on this line:

$body = $arguments['body'];

    if ( is_array( $body ) )
	{
        $body = implode('', $body);  // not always, but sometimes i get "Array to string conversion" notice
    }

I'm not yet ready for WP 5.8, since big changes in widgets and my woocommerce plugins, etc

@carlalexander
Copy link
Author

@Lolosan do you still get it with the updated code?

@Lolosan
Copy link

Lolosan commented Aug 12, 2021

@Lolosan do you still get it with the updated code?

Thank you for the updated code, I'm testing in a woocommerce site with heavy use of APIs and everything seems great

This fix already comes with WP5.8, right? No need to use it after?

@carlalexander
Copy link
Author

@Lolosan That's correct 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment