Skip to content

Instantly share code, notes, and snippets.

@xexu xexu/composer.json Secret
Created Aug 8, 2018

Embed
What would you like to do?
{
"require": {
"guzzlehttp/guzzle": "^6.3",
"symfony/console": "^4.1"
}
}
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
(new Application('wp-api', '1.0.0'))
->register('createpost')
->setCode(function (InputInterface $input, OutputInterface $output) {
$now = new \DateTime();
$title = 'Hello! ' . $now->format('Y-m-d H:i:s');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://localhost/wp-json/wp/v2/posts', [
'auth' => ['user', 'password'],
'form_params' => [
'title' => $title,
'content' => 'This is the body of "' . $title . '"',
]
]);
$object = json_decode($response->getBody());
$output->writeln('Post: #'. $object->id .' '. $object->title->raw . ' created successfully');
})
->getApplication()
->setDefaultCommand('createpost', true)
->run();
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
(new Application('wp-api', '1.0.0'))
->register('publish-all')
->setCode(function (InputInterface $input, OutputInterface $output) {
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://localhost/wp-json/wp/v2/posts', [
'auth' => ['user', 'password'],
'query' => [
'status' => 'draft',
]
]);
$object = json_decode($response->getBody());
array_map(function ($element) use ($client) {
$responsePublish = $client->request('POST', 'http://localhost/wp-json/wp/v2/posts/' . $element->id, [
'auth' => ['user', 'password'],
'form_params' => [
'status' => 'publish',
]
]);
}, $object);
})
->getApplication()
->setDefaultCommand('publish-all', true)
->run();
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
(new Application('wp-api', '1.0.0'))
->register('recent')
->setCode(function (InputInterface $input, OutputInterface $output) {
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://localhost/wp-json/wp/v2/posts', [
'auth' => ['user', 'password'],
'query' => [
'status' => 'publish',
'per_page' => 2,
'page' => 1,
]
]);
$object = json_decode($response->getBody());
$table = new Table($output);
$table->setHeaders(array(
array(new TableCell('Recent posts', array('colspan' => 3))),
array('Post Id', 'Post Date', 'Post Title'),
));
array_map(function ($element) use ($table) {
$table->addRow(array($element->id, $element->date, $element->title->rendered));
}, $object);
$table->render();
})
->getApplication()
->setDefaultCommand('recent', true)
->run();
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
(new Application('wp-api', '1.0.0'))
->register('whoami')
->setCode(function (InputInterface $input, OutputInterface $output) {
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://localhost/wp-json/wp/v2/users/me', [
'auth' => ['user', 'password'],
]);
$object = json_decode($response->getBody());
$output->writeln('You are: #' . $object->id . ' ' . $object->name);
})
->getApplication()
->setDefaultCommand('whoami', true)
->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.