I spent a lot of time to translate my previous PHP code from cURL in Guzzle Http. So, beware because if you use Guzzle you have to use a request in multipart. For example, this was my previous code:
// Action used in curl
$action = 'theme_information';
// Args
$args = [
'slug' => "theme_slug", // replace with your theme slug
'fields' => array(
'description' => true,
'sections' => true,
'rating' => true,
'ratings' => true,
'downloaded' => true,
'downloadlink' => true,
'last_updated' => true,
'tags' => true,
'template' => true,
'parent' => true,
'versions' => true,
'screenshot_url' => true,
),
];
// postfields
$postfields = 'action=' . $action . '&request=' . serialize( (object) $args );
$ch = curl_init( "http://api.wordpress.org/themes/info/1.0/" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postfields );
$res = curl_exec( $ch );
curl_close( $ch );
// final info
$theme = unserialize( $res );
This is the Guzzle version. It works!
// default wp endpoint for theme
$wordpressEndpoint = "http://api.wordpress.org/themes/info/1.0/";
// Action used in curl
$action = 'theme_information';
// Args
$args = [
'slug' => "theme_slug", // replace with your theme slug
'fields' => array(
'description' => true,
'sections' => true,
'rating' => true,
'ratings' => true,
'downloaded' => true,
'downloadlink' => true,
'last_updated' => true,
'tags' => true,
'template' => true,
'parent' => true,
'versions' => true,
'screenshot_url' => true,
),
];
// create an instance of Guzzle client
$httpClient = new \GuzzleHttp\Client();
// post in multipart
$response = $httpClient->request( "POST", $wordpressEndpoint, [
'multipart' => [
[
'name' => 'action',
'contents' => $action
],
[
'name' => 'request',
'contents' => serialize( (object) $args )
],
]
] );
// get body and contents
$body = $response->getBody();
$res = $body->getContents();
// wp response in serialize...
$theme = unserialize( $res );
As usual, please don’t hesitate to leave any questions or comments in the feed below, and I’ll aim to respond to each of them.