2

I tried to get data from outside webservice. I got API url like http://wsq.check.com:50001/Services/appsspg.asmx/GetActionLogand the data for the function is $data = array("parameter1" => "1");. So, how could i get the data from that external webservice?

1 Answer 1

4

To access external webservice, you can use Zend HTTP Client class, using the below code:

protected $_httpClientFactory;

public function __construct(
    \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
) {
    $this->_httpClientFactory   = $httpClientFactory;
}

/**
 * Function to create CURL Request to External Server
 */
public function connect()
{
        $data['parameter1'] = 1;

        $client = $this->_httpClientFactory->create();
        $client->setUri($url);
        $client->getUri()->setPort($port);
        $client->setConfig(['timeout' => 300]);
        $client->setHeaders(['Content-Type: application/json', 'Accept: application/json']);
        $client->setMethod(\Zend_Http_Client::POST);
        $client->setRawData(json_encode($data));

        try {
            $responseBody = $client->request()->getBody();

            echo '<pre>';
            print_r($responseBody);
            die;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }
}
5

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.