2

I am trying to create new custom SOAP API but it returns following error "SoapClient::SoapClient(): $wsdl must be string or null"

etc/webapi.xml :

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route method="GET" url="/V1/mageplaza-helloworld/posts">
    <service class="Mageplaza\HelloWorld\Api\PostManagementInterface" method="getPost"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

Mageplaza/HelloWorld/Api/PostManagementInterface:

<?php
  namespace Mageplaza\HelloWorld\Api;

  interface PostManagementInterface {

    public function getPost();
 } 
?>

etc/di.xml:

<?xml version="1.0" ?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Mageplaza\HelloWorld\Api\PostManagementInterface" type="Mageplaza\HelloWorld\Model\PostManagement"/>
 </config>

Model/PostManagement.php

<?php
 namespace Mageplaza\HelloWorld\Model;

 class PostManagement {

/**
 * {@inheritdoc}
 */
public function getPost()
{
    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom-api.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info("Did't generated the session id");

    return 'api GET return the';
    }
 }

Api request:

    $name = array("param"=>"Jim");

    $wsdlUrl = new SoapClient("http://127.0.0.1/magento201/soap/default?wsdl&services=mageplazaHelloWorldPostManagementV1");
    $context = stream_context_create($opts);
    try{
        $soapClient = new \Zend\Soap\Client($wsdlUrl);
        $soapClient->setSoapVersion(SOAP_1_2);
        $soapClient->setStreamContext($context);
        $soapResponse = $soapClient->mageplazaHelloWorldPostManagementV1GetPost($name);
        echo "<pre>"; var_dump($soapResponse);
    }catch(Exception $e){
        echo "Exception Error:------".$e->getMessage();
    }
  • It means that you WSDL route was not acceded or working, It couldn't connect to that URL. – Skatox May 23 '17 at 13:11
0

Your error message is referred to $soapClient = new \Zend\Soap\Client($wsdlUrl); and $wsdlUrl is not a string but a SoapClient. I mean you are using two SoapClients when you need only one.

Try to change this line:

 $wsdlUrl = new SoapClient("http://127.0.0.1/magento201/soap/default?wsdl&services=mageplazaHelloWorldPostManagementV1");

into this one:

$wsdlUrl = "http://127.0.0.1/magento201/soap/default?wsdl&services=mageplazaHelloWorldPostManagementV1";

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.