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?

3

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, privacy policy and cookie policy

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