0

I'm trying to use Varien_Http_Client for calling external API, here's the code:

    $httpClient = new Varien_Http_Client();
    $httpClient->setUri('https://api-host/api/endpoint');
    $httpClient->setMethod(Zend_Http_Client::GET)
    $httpClient->setParameterGet([
        'name' => 'John',
        'gender' => 'male',
    ]);

    $params = $httpClient->getUri()->getQuery();

The problem is $httpClient->getUri()->getQuery() returns nothing if I set the GET parameters using setParameterGet() like above, shouldn't it suppose to return name=John&gender=male?

Updated: getQuery() after calling request, getting same result

    $httpClient = new Varien_Http_Client();
    $httpClient->setUri('https://api-host/api/endpoint');
    $httpClient->setMethod(Zend_Http_Client::GET)
    $httpClient->setParameterGet([
        'name' => 'John',
        'gender' => 'male',
    ]);

    try {
        $response = $httpClient->request();
        Mage::log($httpClient->getUri()->getQuery());
    } catch (Exception $e) {
        Mage::log($e);
        return $e;
    }

One more thing, it works if I set the parameters this way

$httpClient->setUri('https://api-host/api/endpoint?name=John&gender=male');
2

because you have not sent the request yet, you should:

$httpClient = new Varien_Http_Client();
$httpClient->setUri('https://api-host/api/endpoint');
$httpClient->setMethod(Zend_Http_Client::GET)
$httpClient->setParameterGet([
    'name' => 'John',
    'gender' => 'male',
]);
$httpClient->request();
$param = $httpClient->getUri()->getQuery();

Before $httpClient->request() is called, the Uri's query is null

  • Yes I have tried that and getting same empty result, please see my updated question. – Nam Dau Mar 10 '16 at 9:34
  • have you tried the another URI ? Seems the api-host/api/endpoint cannot access. I have tested with google.com and it's worked. – Nam Luuduc Mar 11 '16 at 10:27

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.