0

I'm working on a custom shipping method linked with an external REST API.

When the seller submits a shipment, an API call is done in order to reserve a shipping course for our carriers.

The problem is that the cURL call fails with this error:

cURL Error: name lookup timed out

I tried something like this :

$curlPostData = $bookCourseRequest; // an object containing the data sent to the API
$curlPostData = json_encode($curlPostData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

$curlQuery = curl_init( //we will assume that the URL is correct );
curl_setopt($curlQuery, CURLOPT_POST, true);
curl_setopt($curlQuery, CURLOPT_POSTFIELDS, $curlPostData);
$header = array("content-type: application/json");
curl_setopt($curlQuery, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlQuery, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlQuery, CURLOPT_CONNECTTIMEOUT, 30);
$curlAnswer = curl_exec($curlQuery);
if (!$curlAnswer)
    mage::log("cURL Error: " . curl_error($curlQuery));
else
    $answer = json_decode($curlAnswer);
curl_close($curlQuery);

Fun fact: this code works well on Magento 2 and on an PHP file unlinked with Magento. With Magento 1.9, there is a timeout error.

I tried the varian way but no luck: still the same error.

Does someone knows how to solve it?

0

Please check below code

Get:-

$client_account_details = new Zend_Http_Client('https://domain.com/');
        $client_account_details->setHeaders('X-Auth-Token','token' );
        $client_account_details->setHeaders('Content-Type', 'application/json');  
        $client_account_details->setMethod(Zend_Http_Client::GET);  
        $account_details_result=$client_account_details->request()->getRawBody();

Post :-

$request_json = json_encode($response_array);
    $client = new Zend_Http_Client();   
    $client->setUri('https://domain.com/'); 
    $client->setHeaders('X-Auth-Token','token' );
    $client->setHeaders(Zend_Http_Client::CONTENT_TYPE, 'application/json'); 
    $client->setMethod(Zend_Http_Client::POST);   
    $client->setRawData($request_json);   
    $response = $client->request()->getBody();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.