Saturday, November 5, 2011

NuSoap with NTML

Last two week i was trying to figure out how to do a POST request to a web service with SOAP and it's security is based on Windows Authentication.
so i create a codeigniter project and get the Nusoap.php and put it as a library for my project. But it dont work as i expected. Every time i did the POST request i end up with  401 Unauthorized 
this is how my codeigniter code looks like
      $wsdl = 'http://billing.waumovil.net/billinggateway.asmx';
        $client = new nusoap_client($wsdl);
        $client->soap_defencoding = 'utf-8';
        $client->setCredentials('', '', 'ntlm');
        $client->setUseCurl(true);
        $client->useHTTPPersistentConnection();
        $client->setCurlOption(CURLOPT_USERPWD, 'Username:Password');
        $client->operations = array('SOAPAction' =>'http://waumovil.net/DoCharge');

        $furl = $wsdl."?op=DoCharge";
        $n_params = array('url' => $furl,'carrierId' => 762, 'msisdn' => 'XXXXXXX', 'productId' => '1', 'description' => 'your description', 'paymentId' => 'yourproductid', 'channelId' => 29);
       $result = $client->call('DoCharge',$n_params,'','http://waumovil.net/DoCharge', array('content-type' => 'UTF-8'), true,null,'rpc','literal');
        echo '<h2>Response</h2>';

        echo '' . htmlspecialchars($client->response, ENT_QUOTES) . '';







for this, the response i got is 401 error code with some nullReferance  exception, its coming from webservice.



to overcome this problem i start using curl and its works, the code i use is giveing bellow





    
function do_charge($data)
    {
       
        $n_params = array('msisdn' => $data['msisdn'], 'carrierId' =>  (int)$data['carrierId'], 'description' => 'your desciption', 'paymentId' => $paymentId, 'productId' => 1, 'channelId' => 29);
        $response = $this->do_soap_request('DoCharge', $n_params);
      }
private function do_soap_request($action, $soap_param = array())
    {
        $headers = array('Method: POST', 'Connection: Keep-Alive', 'User-Agent: PHP-SOAP-CURL', 'Content-Type: text/xml; charset=utf-8', 'SOAPAction: "http://waumovil.net/' . $action . '"', 'Host: billing.waumovil.net');
        $request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:waum=\"http://waumovil.net/\">";
        $request .= "<soapenv:Header/>";
        $request .= "    <soapenv:Body>";
        $request .= "        <waum:" . $action . ">";
        foreach($soap_param as $key => $value)
        {
            $request .= "            <waum:" . $key . ">" . $value . "</waum:" . $key . ">";
        }
        $request .= "        </waum:" . $action . ">";
        $request .= "    </soapenv:Body>";
        $request .= "</soapenv:Envelope>";
        $this->__last_request_headers = $headers;
        $ch = curl_init("http://billing.waumovil.net/billinggateway.asmx");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
        curl_setopt($ch, CURLOPT_USERPWD, $this->UserName . ":" . $this->password);
        $response = curl_exec($ch);
        return $response;
    }
This one works fine. So use Curl.