我正在开发一个Wordpress插件,用于检查是否在Magento v2网上商店中添加了一个项目,并从该项目中获取信息并将其插入到Wordpress中。
我正在制作PHP课程并遇到了一些麻烦,我一直在盯着自己。我不知道我做错了什么。
首先,我在Magento V2中进行了集成,并在我的Wordpress插件中使用了API密钥。
我创建的类处理API调用并返回数据。
class MagentoIntegration {
/**
* Constructor
* @param array $keys Contains Access and Consumers keys
* @param array $config Contains URL, Wordpress options
*/
function __construct($keys, $config)
{
$this->keys = (object) $keys;
$this->config = (object) $config;
$this->user = $this->keys->username;
$this->password = $this->keys->password;
$this->url = $this->config->url;
$this->credentials = array("username" => $this->user, "password" => $this->password);
$this->signature = array();
}
/**
* Get Magento v2 Access Token
* @return string Returns the requested Access Token
*/
public function getToken() {
$this->token = $this->cUrlRequest($this->url . '/rest/V1/integration/admin/token', null, 'POST');
return json_decode($this->token);
}
/**
* Gets and returns all products
* @param string $token Access Token
* @return array Returns all products
*/
public function getAllProducts($token) {
$this->products = $this->cUrlRequest($this->url . '/rest/V1/products?searchCriteria=', $token);
return $this->products;
}
/**
* Returns all categories
* @param string $token Access Token
* @return array Returns all categories
*/
public function getAllCategories($token) {
$this->categories = $this->cUrlRequest($this->url . '/rest/V1/categories', $token);
return $this->categories;
}
/**
* Handles the request using cUrl
* @param string $url Magento v2 API url
* @param string $token Access Token
* @param string $method Method (GET, POST) default: GET
* @return array Result
*/
private function cUrlRequest($url, $token = null, $method = 'GET') {
$this->url = $url;
$this->method = $method;
$this->token = $token;
$this->signature = json_encode($this->credentials);
$curlConfig = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $this->method,
CURLOPT_URL => $this->url,
);
if(!$this->token) { // Only if $token is requested do this
$curlConfig[CURLOPT_POSTFIELDS] = $this->signature;
$curlConfig[CURLOPT_HTTPHEADER] = array('Content-Type: application/json','Content-Length: ' . strlen($this->signature));
} else {
$curlConfig[CURLOPT_HTTPHEADER] = array('Authorization: Bearer ' . $this->token);
}
$ch = curl_init();
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
在这里,我对Class
进行了几次调用<?php
$keys = array(/* contains access and consumer keys, login information */);
$config = array(/* contains config like baseurl */);
$magento = new MagentoIntegration($keys, $config);
$token = $magento->getToken();
$products = $magento->getAllProducts($token);
$categories = $magento->getAllCategories($token);
echo $products;
echo $categories;
$token
变量获取一个令牌,因此可行。
变量$products
和$categories
都返回{"message":"Request does not match any route."}
。
我的问题是我哪里出错了?为什么它不符合路线?