Hi all,
I need help with this script, I was using it a long time ago.
After the upgrade from 7.5.5 of the Espocrm, to 8.x, my API, which was using this script, doesn't work anymore.
I don't find any tutorials that are working or helping me with this problem.
I tried to install the composer with Require Espo... but I think I didn't succeed in installing and making it work...
I was using this script to take data from the CRM, and i was working with data. In my browser, a simple url like ".../api/v1/Invoices" is working, but when is my second virtual server try to take data , it doesn't work.
It can be my .htaccess or something with my apache config too...
My error is :
My php script wich was working
An example how i was using it for post:
An example how i was using it for get:
Thank you for helping me
I need help with this script, I was using it a long time ago.
After the upgrade from 7.5.5 of the Espocrm, to 8.x, my API, which was using this script, doesn't work anymore.
I don't find any tutorials that are working or helping me with this problem.
I tried to install the composer with Require Espo... but I think I didn't succeed in installing and making it work...
I was using this script to take data from the CRM, and i was working with data. In my browser, a simple url like ".../api/v1/Invoices" is working, but when is my second virtual server try to take data , it doesn't work.
It can be my .htaccess or something with my apache config too...
My error is :
Code:
[B]Fatal error[/B]: Uncaught Exception: EspoClient: Unknown Error in /mnt/data/www/html/billManager/EspoApiClient.php:150 Stack trace: #0
PHP Code:
<?php
class EspoApiClient
{
private $url;
private $userName = null;
private $password = null;
protected $urlPath = '/api/v1/';
private $lastCh;
private $lastResponse;
private $apiKey = null;
private $secretKey = null;
public function __construct($url = null, $userName = null, $password = null)
{
if (isset($url)) {
$this->url = $url;
//echo '<br> Url: '.$url;
}
if (isset($userName)) {
$this->userName = $userName;
}
if (isset($password)) {
$this->password = $password;
}
}
public function setUrl($url)
{
$this->url = $url;
//echo '<br> Url: '.$url;
}
public function setUserName($userName)
{
$this->userName = $userName;
//echo '<br> Username: '.$userName;
}
public function setPassword($password)
{
$this->password = $password;
}
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
//echo '<br> ApiKey: '.$apiKey;
}
public function setSecretKey($secretKey)
{
$this->secretKey = $secretKey;
}
/**
* Send request to EspoCRM
*
* @param string $method
* @param string $action
* @param array|null $data
*
* @return array | \Exception
*/
public function request($method, $action, array $data = null)
{
//echo '<br>'.$method.' - '.$action;
$method = strtoupper($method);
$this->checkParams();
$this->lastResponse = null;
$this->lastCh = null;
$url = $this->normalizeUrl($action);
$ch = curl_init($url);
$headerList = [];
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($this->userName && $this->password) {
curl_setopt($ch, CURLOPT_USERPWD, $this->userName.':'.$this->password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
} else if ($this->apiKey && $this->secretKey) {
$string = $method . ' /' . $action;
$authPart = base64_encode($this->apiKey . ':' . hash_hmac('sha256', $string, $this->secretKey, true));
$authHeader = 'X-Hmac-Authorization: ' . $authPart;
$headerList[] = $authHeader;
} else if ($this->apiKey) {
$authHeader = 'X-Api-Key: ' . $this->apiKey;
$headerList[] = $authHeader;
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
if ($method != 'GET') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
if (isset($data)) {
if ($method == 'GET') {
curl_setopt($ch, CURLOPT_URL, $url. '?' . http_build_query($data));
} else {
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$headerList[] = 'Content-Type: application/json';
$headerList[] = 'Content-Length: ' . strlen($payload);
}
}
if (!empty($headerList)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerList);
}
$this->lastResponse = curl_exec($ch);
$this->lastCh = $ch;
$parsedResponse = $this->parseResponce($this->lastResponse);
$responseCode = $this->getResponseHttpCode();
$responseContentType = $this->getResponseContentType();
if ($responseCode == 200 && !empty($parsedResponse['body'])) {
curl_close($ch);
if ($responseContentType === 'application/json') {
return json_decode($parsedResponse['body'], true);
}
return $parsedResponse['body'];
}
$header = $this->normalizeHeader($parsedResponse['header']);
$errorMessage = !empty($header['X-Status-Reason']) ? $header['X-Status-Reason'] : 'EspoClient: Unknown Error';
curl_close($ch);
throw new \Exception($errorMessage, $responseCode);
}
public function getResponseContentType()
{
return $this->getInfo(CURLINFO_CONTENT_TYPE);
}
public function getResponseTotalTime()
{
return $this->getInfo(CURLINFO_TOTAL_TIME);
}
public function getResponseHttpCode()
{
return $this->getInfo(CURLINFO_HTTP_CODE);
}
protected function normalizeUrl($action)
{
return $this->url . $this->urlPath . $action;
}
protected function checkParams()
{
$paramList = [
'url'
];
foreach ($paramList as $name) {
if (empty($this->$name)) {
throw new \Exception('EspoClient: Parameter "'.$name.'" is not defined.');
}
}
return true;
}
protected function getInfo($option)
{
if (isset($this->lastCh)) {
return curl_getinfo($this->lastCh, $option);
}
}
protected function parseResponce($response)
{
$headerSize = $this->getInfo(CURLINFO_HEADER_SIZE);
return [
'header' => trim( substr($response, 0, $headerSize) ),
'body' => substr($response, $headerSize),
];
}
protected function normalizeHeader($header)
{
preg_match_all('/(.*): (.*)\r\n/', $header, $matches);
$headerArray = array();
foreach ($matches[1] as $index => $name) {
if (isset($matches[2][$index])) {
$headerArray[$name] = trim($matches[2][$index]);
}
}
return $headerArray;
}
}
?>
PHP Code:
$response = $client->request('POST', 'Invoice', [
'name' => htmlspecialchars($_POST['name']),
'receiveAmount' => htmlspecialchars($_POST['amount']),
'description' => htmlspecialchars($_POST['description']),
'incomeOutcome' => $incomeOutcome,
'accountId' => htmlspecialchars($accountChoise),
'financialPlanID' => $planID,
'createdAt' => date('Y-m-d'),
'dateInvoiced' => date_format($date, 'Y-m-d'),
]);
PHP Code:
$requestAccountParams = [
'select' => 'id,name',
'orderBy' => 'name',
'order' => 'asc',];
listSelected ($client->request('GET', 'Account',$requestAccountParams),$_SESSION['UserData']['account'], "account");
Thank you for helping me
Comment