HubBox Error: Bad Response Code

Problem Statement:  Error coming in the response of Authentication API of HubBox ie. “HubBox Api Error: Bad Response Code

Solution:  If you are facing issue as shown below while you are checking Authentication API, it may be because of Authentication API is verifying SSL certificate for connection.
HubBox Error: Bad Response Code

For this, you can turn off the SSL verification in the CURL request by adding below lines.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

Make the change in the curl request of Authentication API in the function refreshToken() at HubBox/PHP/HubBox/Authentication.php as shown below:

$ch = curl_init();
$post_string = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_USERPWD, $this->_magento_user . ":" . $this->_magento_key);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$body = substr($output, $header_size);
curl_close($ch);

If this was the issue, you will need to add these lines in all APIs of HubBox Create Parcel, Cancel Parcel, Add Tracking and Collectpoint APIs as described below.

Create Parcel API: Make the change in the curl request of Create Parcel API in the function sendOrder() at HubBox/PHP/HubBox/Parcel.php as shown below:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getOrderUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-type: application/json',
'Authorization: Bearer '. $token,
'Content-Length: ' . strlen($body)
)
);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($output, $header_size);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

Cancel Parcel API: Make the change in the curl request of Cancel Parcel API in the function cancelOrder() at HubBox/PHP/HubBox/Parcel.php as shown below:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCancelUrl($hubBoxParcelId));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-type: application/json',
'Authorization: Bearer '. $token,
'Content-Length: ' . strlen($postBody)
)
);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($output, $header_size);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

Add Tracking API: Make the change in the curl request of Add Tracking API in the function addTracking() at HubBox/PHP/HubBox/Parcel.php as shown below:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getTrackingUrl($hubBoxParcelId));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-type: application/json',
'Authorization: Bearer '. $token,
'Content-Length: ' . strlen($postBody)
)
);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($output, $header_size);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

Collectpoint API: Make the change in the curl request of Collectpoint API in the function getCollectPoint() at HubBox/PHP/HubBox/CollectPoints.php as shown below:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCollectPointUrl() . '/' . $id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

// if we have private collect points we must authenticate the request
if ($this->_config->hasPrivateCps()) {
$header[] = "Authorization: Bearer " . $this->_auth->getAccessToken();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}

$output = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($output, $header_size);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

For more related articles please visit here: HubBox Error: Wrong product data in the response of Create Parcel API

Akash Roshan

Akash is a PHP developer and wants to share his knowledge through Blogs. He is currently growing in the IT industry while fulfilling his own goals.

Leave a Reply

Your email address will not be published. Required fields are marked *