Skip to content

Instantly share code, notes, and snippets.

@copitz
Created October 7, 2020 11:51
Show Gist options
  • Save copitz/b94334d9bbc452814b0444af5d01c146 to your computer and use it in GitHub Desktop.
Save copitz/b94334d9bbc452814b0444af5d01c146 to your computer and use it in GitHub Desktop.
Call Magento2 REST API from command line without curl
<?php
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\ErrorHandler;
use Magento\Framework\App\Http;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\Webapi\Rest\Request;
use \Magento\Framework\Webapi\Rest\Response;
use Zend\Http\Header\ContentType;
require 'app/bootstrap.php';
$handler = new ErrorHandler();
set_error_handler([$handler, 'handler']);
echo "[";
register_shutdown_function(function () { echo "\n]"; });
function send(Response $response) {
static $isFirst = true;
$isError = !$response->isSuccess() || $response->isException();
if (!$isError && !$GLOBALS["curl_return_result"]) {
return;
}
if (!$isFirst) {
echo ",";
}
$isFirst = false;
echo "\n";
$response->sendResponse();
if ($isError && !$GLOBALS["curl_continue_on_errors"]) {
exit(1);
}
}
function curl($path, $method, $payload) {
$bootstrap = Bootstrap::create(BP, []);
$om = $bootstrap->getObjectManager();
$om->configure([
'preferences' => [
'Magento\Framework\Authorization\PolicyInterface' => 'Magento\Framework\Authorization\Policy\DefaultPolicy'
]
]);
$app = $bootstrap->createApplication(Http::class);
/** @var Request $restRequest */
$restRequest = $om->get(Request::class);
$restRequest->getHeaders()->addHeader(new ContentType("application/json"));
$restRequest->setMethod($method);
$restRequest->setContent($payload);
/** @var RequestHttp $request */
$request = $om->get(RequestHttp::class);
$request->setPathInfo($path);
$response = $app->launch();
if ($response instanceof Response) {
send($response);
} else {
echo "{\"message\": \"Unexpected response type: " . get_class($response) . " - wrong path?\"}";
exit(1);
}
}
$GLOBALS["curl_return_result"] = false;
$GLOBALS["curl_continue_on_errors"] = false;
curl("/rest/all/V1/products/FULL_PRODUCT_MODEL", "PUT", <<<PAYLOAD
{"product":{"sku":"FULL_PRODUCT_MODEL","status":0,"extension_attributes":{"stock_item":{"use_config_backorders":1}}}}
PAYLOAD
);
curl("/all/V1/products/FULL_INCLOMPLETE_PRODUCT", "PUT", <<<PAYLOAD
{"product":{"sku":"FULL_INCLOMPLETE_PRODUCT","status":1,"visibility":1,"extension_attributes":{"stock_item":{"qty":43,"backorders":0,"use_config_backorders":0,"is_in_stock":true}}}}
PAYLOAD
);
curl("/all/V1/products/FULL_SIMPLE_PRODUCT", "PUT", <<<PAYLOAD
{"product":{"sku":"FULL_SIMPLE_PRODUCT","visibility":4,"extension_attributes":{"stock_item":{"qty":1,"backorders":1,"use_config_backorders":0,"is_in_stock":true}}}}
PAYLOAD
);
curl("/all/V1/products/MINIMAL_PRODUCT", "PUT", <<<PAYLOAD
{"product":{"sku":"MINIMAL_PRODUCT","visibility":2,"extension_attributes":{"stock_item":{"backorders":1,"use_config_backorders":0}}}}
PAYLOAD
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment