4

In magento 2.4.5, I use some function below to connect with QBO sandbox ZendClient and it's working perfectly:

  • setUri()
  • setConfig(array())
  • setHeaders()
  • setRawData()
  • request($method) ($method can be GET, POST,...)

In magento 2.4.6, ZendClient is replacement by LaminasClient. I have try function from new class LaminasClient:

  • setUri() => setUri()
  • setConfig(array()) => setOptions(array())
  • setHeaders() => setHeaders()
  • setRawData() => setRawBody()
  • request($method) => send()

But the response have status code is 401. I have try debug into function send() and get $uri, $header to test in postman and postman return correctly information of sandbox QBO. I don't know why this happen. Can anyone give me some advices? Thanks.

3 Answers 3

8

I have try replacement ZendClient with class Laminas\Http\Client and it working. This would help me compatible module with Magento version 2.4.6 and the old version 2.4.x can use it too.

Some change function:

  • setUri() => setUri()
  • setConfig(array()) => setOptions(array())
  • setHeaders() => setHeaders()
  • setRawData() => setRawBody()
  • request($method) => send() (before use send(), you need set method with function setMethod($method) first.

Hope this answer could help someone.

1
  • 2
    Also note, in response, isSuccessful() changed to isSuccess() and getStatus() changed to getStatusCode(). Please refer classes located at vendor/laminas/laminas-http/src
    – Deeps
    Commented Jun 12, 2023 at 5:51
1
    /**
     * @var \Magento\Framework\HTTP\LaminasClient $client
     */
    $client = $this->httpClientFactory->create();
    $client->setOptions(['adapter' => \Laminas\Http\Client\Adapter\Curl::class]);
0

For a solution that works on pre Magento 2.4.6 versions, you could look at implementing \Magento\Framework\HTTP\Client\Curl This would allow those who are currently on Magento 2.4.5 and Magento 2.4.4 to remove ZendClient in preparation for Magento 2.4.6 or 2.4.7. For put requests that are RawData, json_encode your $param array.

 //    $params = [];
     $userName ='username'; //example hardcoded
     $password ='password'; //example hardcoded

     $url = 'https://localhost';

     $curl = new \Magento\Framework\HTTP\Client\Curl;
     $curl->setCredentials($userName, $password);
     $curl->addHeader('Content-Type', 'application/json');
     $curl->addHeader('Accept', 'application/json');
     $curl->get($url);
    //  $curl->post($url, $params);
     $response = json_decode($curl->getBody(), true);
     print_r($response);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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