0

I created a Magento 2.0 module and it calls an external web service with the command \Zend_XmlRpc_Client.

/* REST */
$client = new \Zend_XmlRpc_Client($this->_scopeConfig->getValue('akria_store/store_view/domain_configurateur').'/api/xmlrpc/');
$session = $client->call('login', array($this->_scopeConfig->getValue('akria_store/soap/user'), $this->_scopeConfig->getValue('akria_store/soap/password')));
$result = $client->call('call', array   ($session, 'configurateur_api.setprojectstate' ,array(array('order_id'=>$order_id,'state' => \Akria\Configurateur\Model\Project::PENDING,'project' => $item->getData('configurateur_project_id')))));

But this function no longer exists.

How can I now call an external web service without using curl? replacement function of \Zend_XmlRpc_Client.

thank you in advance.

1 Answer 1

0

SOAP:

use Magento\Framework\Webapi\Soap\Client

 public function __construct(
    \Magento\Framework\Webapi\Soap\ClientFactory $soap
) {
    $this->soap = $soap;
}

and in function

public function makeRequest() {
     $opts = [
                'http' => [
                    'user_agent' => 'PHPSoapClient'
                ]
            ];
     $context = stream_context_create($opts);
     $url = 'http://someurl.com';
     $soapClientOptions = [
                'stream_context' => $context,
                'cache_wsdl' => 0
            ];
     $client = $this->soap->create($url, $soapClientOptions);
     $content = ['test' => '123'];
     $result = $client->functionFromApi($options);
}

Curl:

Use Magento\Framework\HTTP\Client\Curl class.

There are some functions for set headers, credentials, body etc.

To make request just create object:

public function construct(
    Curl $curl,
) {
    $this->curl = $curl;
}

and in function

public function makeRequest() {
    $url = 'http://someurl.com';
    $content = ['test' => '123'];

    //to make post request
    $this->curl->post($url, $content);

    //to make get request
    $this->curl->get($url, $content);

    //to get response from request
    $response = $this->curl->getBody();
}
1
  • 1
    Thx, use curl I find it very ugly so I'm going to use SOAP
    – S8N
    Aug 2, 2019 at 7:57

Your Answer

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

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