Site icon VR SoftCoder

How to integrate Binect APIs in php

How to integrate Binect APIs in php

How to integrate Binect APIs in php

In this tutorial, we are going to learn How to integrate Binect APIs in PHP. First thing you need to create the authorization code using your username and password like below:-

Basic YW51anN0YXBsZUBnbWFpbC5jb206QW51akAxMjM=

1) Get user data

Through the first binect API, we will get user data using PHP curl. You need to set the authorization header for every API call. Below is the complete code to get the user information:-

 "https://app.binect.de/binectapi/v1/accounts/personaldata",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic YW51anN0YXBsZUBnbWFspbC5jb206QW51akAxMjM=",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

The response you will get:-

{"debitornumber":"121002570","email":"abc@gmail.com","country":"Germany"}

2) Upload a Document

Before uploading a document you need to create the document in a proper format. You can download the sample here. Below is the complete code to upload a document through binect API:–

 
  array (
    'filename' => 'final.pdf',
    'content' => $convert,
  ),
);
$jsonpost = json_encode($postarray);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://app.binect.de/binectapi/v1/documents",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $jsonpost,
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic YW51anN0YXBsdZUBnbWFpbC5jb206QW51akAxMjM=",
    "cache-control: no-cache",
    "content-type: application/json",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

The response you will get:-

{"id":6006627,"filename":"myf.pdf","numberOfPages":1,"status":{"code":2,"text":"versandbereit"},"documentType":"LETTER","letter":{"letterType":"LETTERDATA","letterData":{"recipientAddress":"\"Regards,\" \"Lily Morgan\" \"School Teacher, UV High School\" \"Assandh karnal, 132046\" \"India\"","price":{"priceBeforeTax":144,"priceAfterTax":167,"unit":"EUROCENT","taxInPercent":16},"international":true,"options":{"simplex":true,"color":false},"attributes":[],"attachments":[]},"errors":[]}}

3) Sending Document

Also when you sending a document, your document should be well formatted with the address and country name in last. You should have enough credit in your account.
below is the completed code to send a document through binect API.

 
  array (
    'filename' => 'myf.pdf',
    'content' => $convert,
  ),
);
$jsonpost = json_encode($postarray);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://app.binect.de/binectapi/v1/sendings/document",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $jsonpost,
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic YW51anN0YXBsZUBnbWFspbC5jb206QW51akAxMjM=",
    "cache-control: no-cache",
    "content-type: application/json",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

The response you will get:-

Bad Request: Error on calculate the price on POST Sending 6006628 with error code '2330': Your pre-paid credit is insufficient. The letters cannot be billed.
Exit mobile version