Cześć.
Próbuję połączyć się z API mojego sklepu w Cupsell. Niestety PHP jest mi obcy, ale od jakiegoś czasu uczę się JS. Więc wygląda to tak, że odpalam sobie plik example.php pobrany z http://cupsell.pl/api-php-sdk/ na wirtualnym serwerze XAMPP, i wyświetla mi się taki błąd:
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\test\CupSellAPI.php on line 61
Plik CupSellAPI.php znajduje się w tym samym folderze co plik example.php Czy ktoś mógłby mi pomóc i podpowie, jak mam nawiązać połączenie z tym API?
example.php :
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CupSell API</title>
</head>
<body>
<pre>
<?php
$config = array(
'api_key' => 'YOUR_API_KEY',
'private_key' => 'YOUR_PRIVATE_KEY'
);
include "CupSellAPI.php";
$api = new CupSellAPI($config);
$result = $api->request("api-test");
print_r($result);
CupSellAPI.php :
<?php
/**
*
* API 1.1.0 PHP-SDK
*
* Class CupSellAPI
*/
class CupSellAPI
{
const TARGET_URL = 'https://cupsell.pl/api/';
private $target = self::TARGET_URL;
private $config;
function __construct($config)
{
$this->config = $config;
}
function request($data, $val = NULL, $data_only = false, $debug = false)
{
if (is_array($val)) {
$get = (isset($val['get'])) ? $val['get'] : NULL;
$post = (isset($val['post'])) ? $val['post'] : NULL;
if (($get == NULL) && ($post == NULL)) {
$post = $val;
}
} else {
$id = $val;
$get = NULL;
$post = NULL;
}
$target = $this->target.$data.(isset($id) && $id ? "/$id" : "")."?";
$excluded_params = array('signature', 'pages_count', 'page');
$params = array(
'api_key' => $this->config['api_key'],
'timestamp' => time(),
'signature' => NULL
);
if (isset($id)) {
$params['id'] = $id;
}
if ($get) {
$params = $params + $get;
}
$params['signature'] = $this->toHash($params,
$this->config['private_key'], $excluded_params, $post);
$response = $this->sendRequest($params, $target, $post);
# DEBUG #
if ($debug) {
echo "DEBUG START \n".htmlentities($response, ENT_QUOTES, "UTF-8")."\nDEBUG END \n";
}
$jd = json_decode($response, true);
return $data_only ? $jd['response']['details']['data'] : $jd['response'];
}
function test()
{
$ret = $this->request("api-test");
return ($ret['status'] == 200 ? true : false);
}
private function toHash(array $params, $private_key, $excluded_params, $post)
{
$string = '';
if ($post) {
$params = $params + $post;
}
ksort($params);
foreach ($params as $key => $val) {
if (!in_array($key, $excluded_params) && !is_null($val)) {
$string .= $key.'='.$val.'#';
}
}
return hash_hmac('sha256', $string, $private_key);
}
private function sendRequest(array $params, $target, $post)
{
foreach ($params as $key => $val) {
if ($val === null) {
unset($params[$key]);
}
}
$target .= http_build_query($params);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host: cupsell.local'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, getcwd() . '/cupsell-api-cert.crt');
if ($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
}
$return = curl_exec($curl);
curl_close($curl);
return $return;
}
}