本文整理汇总了PHP中WP_CLI\Utils\http_request函数的典型用法代码示例。如果您正苦于以下问题:PHP http_request函数的具体用法?PHP http_request怎么用?PHP http_request使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了http_request函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: install
/**
* Install a WP-CLI package.
*
* Packages are required to be a valid Composer package, and can be
* specified as:
*
* * Package name from WP-CLI's package index.
* * Git URL accessible by the current shell user.
* * Path to a directory on the local machine.
* * Local or remote .zip file.
*
* When installing a local directory, WP-CLI simply registers a
* reference to the directory. If you move or delete the directory, WP-CLI's
* reference breaks.
*
* When installing a .zip file, WP-CLI extracts the package to
* `~/.wp-cli/packages/local/<package-name>`.
*
* ## OPTIONS
*
* <name|git|path|zip>
* : Name, git URL, directory path, or .zip file for the package to install.
* Names can optionally include a version constraint
* (e.g. wp-cli/server-command:@stable).
*
* ## EXAMPLES
*
* # Install the latest development version from the package index.
* $ wp package install wp-cli/server-command
* Installing package wp-cli/server-command (dev-master)
* Updating /home/person/.wp-cli/packages/composer.json to require the package...
* Using Composer to install the package...
* ---
* Loading composer repositories with package information
* Updating dependencies
* Resolving dependencies through SAT
* Dependency resolution completed in 0.005 seconds
* Analyzed 732 packages to resolve dependencies
* Analyzed 1034 rules to resolve dependencies
* - Installing package
* Writing lock file
* Generating autoload files
* ---
* Success: Package installed.
*
* # Install the latest stable version.
* $ wp package install wp-cli/server-command:@stable
*
* # Install a package hosted at a git URL.
* $ wp package install git@github.com:runcommand/hook.git
*
* # Install a package in a .zip file.
* $ wp package install google-sitemap-generator-cli.zip
*/
public function install($args, $assoc_args)
{
list($package_name) = $args;
$git_package = $dir_package = false;
$version = 'dev-master';
if ('.git' === strtolower(substr($package_name, -4, 4))) {
$git_package = $package_name;
preg_match('#([^:\\/]+\\/[^\\/]+)\\.git#', $package_name, $matches);
if (!empty($matches[1])) {
$package_name = $matches[1];
} else {
WP_CLI::error("Couldn't parse package name from expected path '<name>/<package>'.");
}
} else {
if (false !== strpos($package_name, '://') && false !== stripos($package_name, '.zip') || pathinfo($package_name, PATHINFO_EXTENSION) === 'zip' && is_file($package_name)) {
// Download the remote ZIP file to a temp directory
if (false !== strpos($package_name, '://')) {
$temp = Utils\get_temp_dir() . uniqid('package_') . ".zip";
$options = array('timeout' => 600, 'filename' => $temp);
$response = Utils\http_request('GET', $package_name, null, array(), $options);
if (20 != substr($response->status_code, 0, 2)) {
WP_CLI::error("Couldn't download package.");
}
$package_name = $temp;
}
$dir_package = Utils\get_temp_dir() . uniqid('package_');
try {
// Extract the package to get the package name
Extractor::extract($package_name, $dir_package);
list($package_name, $version) = self::get_package_name_and_version_from_dir_package($dir_package);
// Move to a location based on the package name
$local_dir = rtrim(WP_CLI::get_runner()->get_packages_dir_path(), '/') . '/local/';
$actual_dir_package = $local_dir . str_replace('/', '-', $package_name);
Extractor::copy_overwrite_files($dir_package, $actual_dir_package);
Extractor::rmdir($dir_package);
// Behold, the extracted package
$dir_package = $actual_dir_package;
} catch (Exception $e) {
WP_CLI::error($e->getMessage());
}
} else {
if (is_dir($package_name) && file_exists($package_name . '/composer.json')) {
$dir_package = $package_name;
if (!Utils\is_path_absolute($dir_package)) {
$dir_package = getcwd() . DIRECTORY_SEPARATOR . $dir_package;
}
//.........这里部分代码省略.........
示例2: get_core_checksums
/**
* Security copy of the core function with Requests - Gets the checksums for the given version of WordPress.
*
* @param string $version Version string to query.
* @param string $locale Locale to query.
* @return bool|array False on failure. An array of checksums on success.
*/
private static function get_core_checksums($version, $locale)
{
$url = $http_url = 'http://api.wordpress.org/core/checksums/1.0/?' . http_build_query(compact('version', 'locale'), null, '&');
if ($ssl = wp_http_supports(array('ssl'))) {
$url = 'https' . substr($url, 4);
}
$options = array('timeout' => 30);
$headers = array('Accept' => 'application/json');
$response = Utils\http_request('GET', $url, null, $headers, $options);
if ($ssl && !$response->success) {
WP_CLI::warning('wp-cli could not establish a secure connection to WordPress.org. Please contact your server administrator.');
$response = Utils\http_request('GET', $http_url, null, $headers, $options);
}
if (!$response->success || 200 != $response->status_code) {
return false;
}
$body = trim($response->body);
$body = json_decode($body, true);
if (!is_array($body) || !isset($body['checksums']) || !is_array($body['checksums'])) {
return false;
}
return $body['checksums'];
}
示例3: get_updates
/**
* Returns update information.
*/
private function get_updates($assoc_args)
{
$url = 'https://api.github.com/repos/wp-cli/wp-cli/releases';
$options = array('timeout' => 30);
$headers = array('Accept' => 'application/json');
$response = Utils\http_request('GET', $url, $headers, $options);
if (!$response->success || 200 !== $response->status_code) {
WP_CLI::error(sprintf("Failed to get latest version (HTTP code %d).", $response->status_code));
}
$release_data = json_decode($response->body);
$updates = array('major' => false, 'minor' => false, 'patch' => false);
foreach ($release_data as $release) {
// Get rid of leading "v" if there is one set.
$release_version = $release->tag_name;
if ('v' === substr($release_version, 0, 1)) {
$release_version = ltrim($release_version, 'v');
}
$update_type = Utils\get_named_sem_ver($release_version, WP_CLI_VERSION);
if (!$update_type) {
continue;
}
if (!empty($updates[$update_type]) && !Comparator::greaterThan($release_version, $updates[$update_type]['version'])) {
continue;
}
$updates[$update_type] = array('version' => $release_version, 'update_type' => $update_type, 'package_url' => $release->assets[0]->browser_download_url);
}
foreach ($updates as $type => $value) {
if (empty($value)) {
unset($updates[$type]);
}
}
foreach (array('major', 'minor', 'patch') as $type) {
if (true === \WP_CLI\Utils\get_flag_value($assoc_args, $type)) {
return !empty($updates[$type]) ? array($updates[$type]) : false;
}
}
if (empty($updates) && preg_match('#-alpha-(.+)$#', WP_CLI_VERSION, $matches)) {
$version_url = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/NIGHTLY_VERSION';
$response = Utils\http_request('GET', $version_url);
if (!$response->success || 200 !== $response->status_code) {
WP_CLI::error(sprintf("Failed to get current nightly version (HTTP code %d)", $response->status_code));
}
$nightly_version = trim($response->body);
if (WP_CLI_VERSION != $nightly_version) {
$updates['nightly'] = array('version' => $nightly_version, 'update_type' => 'nightly', 'package_url' => 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar');
}
}
return array_values($updates);
}
示例4: get_updates
/**
* Returns update information
*/
private function get_updates($assoc_args)
{
$url = 'https://api.github.com/repos/wp-cli/wp-cli/releases';
$options = array('timeout' => 30);
$headers = array('Accept' => 'application/json');
$response = Utils\http_request('GET', $url, $headers, $options);
if (!$response->success || 200 !== $response->status_code) {
WP_CLI::error(sprintf("Failed to get latest version (HTTP code %d)", $response->status_code));
}
$release_data = json_decode($response->body);
$current_parts = explode('.', WP_CLI_VERSION);
$updates = array();
foreach ($release_data as $release) {
$release_version = $release->tag_name;
// get rid of leading "v"
if ('v' === substr($release_version, 0, 1)) {
$release_version = ltrim($release_version, 'v');
}
// don't list earlier releases
if (version_compare($release_version, WP_CLI_VERSION, '<=')) {
continue;
}
$release_parts = explode('.', $release_version);
$update_type = 'minor';
if ($release_parts[0] === $current_parts[0] && $release_parts[1] === $current_parts[1]) {
$update_type = 'patch';
}
if (!(\WP_CLI\Utils\get_flag_value($assoc_args, 'patch') && 'patch' !== $update_type) && !(\WP_CLI\Utils\get_flag_value($assoc_args, 'patch') === false && 'patch' === $update_type) && !(\WP_CLI\Utils\get_flag_value($assoc_args, 'minor') && 'minor' !== $update_type) && !(\WP_CLI\Utils\get_flag_value($assoc_args, 'minor') === false && 'minor' === $update_type) && !$this->same_minor_release($release_parts, $updates)) {
$updates[] = array('version' => $release_version, 'update_type' => $update_type, 'package_url' => $release->assets[0]->browser_download_url);
}
}
return $updates;
}
示例5: get_updates
/**
* Returns update information
*/
private function get_updates($assoc_args)
{
global $wp_version;
$versions_path = ABSPATH . 'wp-includes/version.php';
include $versions_path;
$url = 'https://api.wordpress.org/core/stable-check/1.0/';
$options = array('timeout' => 30);
$headers = array('Accept' => 'application/json');
$response = Utils\http_request('GET', $url, $headers, $options);
if (!$response->success || 200 !== $response->status_code) {
WP_CLI::error("Failed to get latest version list.");
}
$release_data = json_decode($response->body);
$release_versions = array_keys((array) $release_data);
usort($release_versions, function ($a, $b) {
return 1 === version_compare($a, $b);
});
$locale = get_locale();
$compare_version = str_replace('-src', '', $GLOBALS['wp_version']);
$updates = array('major' => false, 'minor' => false);
foreach ($release_versions as $release_version) {
$update_type = Utils\get_named_sem_ver($release_version, $compare_version);
if (!$update_type) {
continue;
}
// WordPress follow its own versioning which is roughly equivalent to semver
if ('minor' === $update_type) {
$update_type = 'major';
} else {
if ('patch' === $update_type) {
$update_type = 'minor';
}
}
if (!empty($updates[$update_type]) && !Comparator::greaterThan($release_version, $updates[$update_type]['version'])) {
continue;
}
$updates[$update_type] = array('version' => $release_version, 'update_type' => $update_type, 'package_url' => $this->get_download_url($release_version, $locale));
}
foreach ($updates as $type => $value) {
if (empty($value)) {
unset($updates[$type]);
}
}
foreach (array('major', 'minor') as $type) {
if (true === \WP_CLI\Utils\get_flag_value($assoc_args, $type)) {
return !empty($updates[$type]) ? array($updates[$type]) : false;
}
}
return array_values($updates);
}
示例6: get_core_checksums
/**
* Security copy of the core function with Requests - Gets the checksums for the given version of WordPress.
*
* @param string $version Version string to query.
* @param string $locale Locale to query.
* @return bool|array False on failure. An array of checksums on success.
*/
private static function get_core_checksums($version, $locale)
{
$url = 'https://api.wordpress.org/core/checksums/1.0/?' . http_build_query(compact('version', 'locale'), null, '&');
$options = array('timeout' => 30);
$headers = array('Accept' => 'application/json');
$response = Utils\http_request('GET', $url, null, $headers, $options);
if (!$response->success || 200 != $response->status_code) {
return false;
}
$body = trim($response->body);
$body = json_decode($body, true);
if (!is_array($body) || !isset($body['checksums']) || !is_array($body['checksums'])) {
return false;
}
return $body['checksums'];
}
示例7: get_api_index
/**
* Get the index data from an API url
*
* @param string $api_url
* @return array|false
*/
private static function get_api_index($api_url)
{
$query_char = false !== strpos($api_url, '?') ? '&' : '?';
$api_url .= $query_char . 'context=help';
$response = Utils\http_request('GET', $api_url);
if (empty($response->body)) {
return false;
}
return json_decode($response->body, true);
}
示例8: do_request
/**
* Do a REST Request
*
* @param string $method
*
*/
private function do_request($method, $route, $assoc_args)
{
if ('internal' === $this->scope) {
$request = new \WP_REST_Request($method, $route);
if (in_array($method, array('POST', 'PUT'))) {
$request->set_body_params($assoc_args);
} else {
foreach ($assoc_args as $key => $value) {
$request->set_param($key, $value);
}
}
$response = rest_do_request($request);
if ($error = $response->as_error()) {
WP_CLI::error($error);
}
return array($response->get_status(), $response->get_data(), $response->get_headers());
} else {
if ('http' === $this->scope) {
$response = Utils\http_request($method, rtrim($this->api_url, '/') . $route, $assoc_args);
$body = json_decode($response->body, true);
if ($response->status_code >= 400) {
if (!empty($body['message'])) {
WP_CLI::error($body['message'] . ' ' . json_encode(array('status' => $response->status_code)));
} else {
switch ($response->status_code) {
case 404:
WP_CLI::error("No {$this->name} found.");
break;
default:
WP_CLI::error('Could not complete request.');
break;
}
}
}
return array($response->status_code, json_decode($response->body, true), $response->headers);
}
}
WP_CLI::error('Invalid scope for REST command.');
}
示例9: array
/**
* Check for update via Github API. Returns the available versions if there are updates, or empty if no update available.
*
* ## OPTIONS
*
* [--patch]
* : Compare only the first two parts of the version number.
*
* [--minor]
* : Compare only the first part of the version number.
*
* [--field=<field>]
* : Prints the value of a single field for each update.
*
* [--fields=<fields>]
* : Limit the output to specific object fields. Defaults to version,update_type,package_url.
*
* [--format=<format>]
* : Accepted values: table, csv, json, count. Default: table
*
* @subcommand check-update
*/
function check_update($_, $assoc_args)
{
$url = 'https://api.github.com/repos/wp-cli/wp-cli/releases';
$options = array('timeout' => 30);
$headers = array('Accept' => 'application/json');
$response = Utils\http_request('GET', $url, $headers, $options);
if (!$response->success || 200 !== $response->status_code) {
WP_CLI::error("Failed to get latest version.");
}
$release_data = json_decode($response->body);
$current_parts = explode('.', WP_CLI_VERSION);
$updates = array();
foreach ($release_data as $release) {
$release_version = $release->tag_name;
// get rid of leading "v"
if ('v' === substr($release_version, 0, 1)) {
$release_version = ltrim($release_version, 'v');
}
// don't list earlier releases
if (version_compare($release_version, WP_CLI_VERSION, '<=')) {
continue;
}
$release_parts = explode('.', $release_version);
$update_type = 'minor';
if ($release_parts[0] === $current_parts[0] && $release_parts[1] === $current_parts[1]) {
$update_type = 'patch';
}
if (!(isset($assoc_args['patch']) && 'patch' !== $update_type) && !(isset($assoc_args['minor']) && 'minor' !== $update_type) && !$this->same_minor_release($release_parts, $updates)) {
$updates[] = array('version' => $release_version, 'update_type' => $update_type, 'package_url' => $release->assets[0]->browser_download_url);
}
}
if ($updates) {
$formatter = new \WP_CLI\Formatter($assoc_args, array('version', 'update_type', 'package_url'));
$formatter->display_items($updates);
} else {
if (empty($assoc_args['format']) || 'table' == $assoc_args['format']) {
WP_CLI::success("WP-CLI is at the latest version.");
}
}
}