6

I've tried almost all the possible solutions I could find but I can't seem to get this script working. Here are some of my previous attempts:

How to use wse-php library to connect to secured webservices via SOAP

https://stackoverflow.com/questions/18952222/soap-error-encoding-object-has-no-createlead-property-php

How to solve Error: SOAP-ERROR: Encoding: object has no 'createLead' property?

How to Consume a secured web service with WS-Security via SOAP [PHP]?

Here's my latest attempt:

<?php  
try    {
$username = 'derek';
$password = 'Momentum1';
$url = "https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/"; 

$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.momentum.co.za/crm/service/application/CRMLeadService/v1.0" xmlns:v11="http://www.momentum.co.za/crm/service/type/application/Lead/v1.0" xmlns:v12="http://www.momentum.co.za/crm/service/type/TitleType/v1.0" xmlns:v13="http://www.momentum.co.za/crm/service/type/LanguageType/v1.0" xmlns:v14="http://www.momentum.co.za/crm/service/type/PreferredContactMethodType/v1.0" xmlns:v15="http://www.momentum.co.za/crm/service/type/CampaignType/v1.0" xmlns:v16="http://www.momentum.co.za/crm/service/type/ProductCategoryType/v1.0">
   <soapenv:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <wsse:UsernameToken>
            <wsse:Username>derek</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Momentum1</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <v1:CreateLeadRequest>
         <createLead>
            <v11:LeadSourceId>23627e70-a29e-e211-b8a8-005056b81ebe</v11:LeadSourceId>
            <v11:AffiliateLeadReference>852800020</v11:AffiliateLeadReference>
            <v11:Title>
               <v12:Code>852800018</v12:Code>
            </v11:Title>
            <v11:Initials>MD</v11:Initials>
            <v11:PreferredName>Marius</v11:PreferredName>
            <v11:FirstName>Marius</v11:FirstName>
            <v11:LastName>Drew</v11:LastName>
            <v11:PreferredCorrespondenceLanguage>
               <v13:Code>852800001</v13:Code>
               <v13:Description>?</v13:Description>
            </v11:PreferredCorrespondenceLanguage>
            <v11:PreferredCommunicationMethod>
               <v14:Code>852800000</v14:Code>
            </v11:PreferredCommunicationMethod>
            <v11:Campaign>
               <v15:Code>95D9042A-440E-E311-A5EB-005056B81EA5</v15:Code>
            </v11:Campaign>
            <v11:HomePhoneNumber>0723621762</v11:HomePhoneNumber>
            <v11:BusinessPhoneNumber>0723621762</v11:BusinessPhoneNumber>
            <v11:MobilePhoneNumber>0723621762</v11:MobilePhoneNumber>
            <v11:EmailAddress>mdrew@gmail.com</v11:EmailAddress>
            <v11:Notes>IMU</v11:Notes>
            <v11:ProductCategories>
               <v16:Code>d000083d-229c-e211-b8a8-005056b81ebe</v16:Code>
            </v11:ProductCategories>
         </createLead>
      </v1:CreateLeadRequest>
   </soapenv:Body>
</soapenv:Envelope>';       

$content = utf8_encode($xml); 
$content_length = strlen($xml);  

$headers = array('Content-Type: application/soap+xml; charset=utf-8', 'Content-Length: ' . $content_length); 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $content); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$output = curl_exec($ch); 
curl_close($ch);  

print $output; 
} catch (Exception $e) { 
    var_dump($e); 
} 
?>

I think there's something wrong with the way I send my message. Is it possible that the problem is my WSDL and its location?

I'm already working on this for a week and I'm getting frustrated already. This WS-Security stuff is a pain. I tried using the suggestions here on StackOverflow such as creating a class which extends to SoapClient but still did not work. I hope somebody could be angel and help me with this. Thanks!

Oh, by the way, here's the RAW XML message request on SOAPUI:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.momentum.co.za/crm/service/application/CRMLeadService/v1.0" xmlns:v11="http://www.momentum.co.za/crm/service/type/application/Lead/v1.0" xmlns:v12="http://www.momentum.co.za/crm/service/type/TitleType/v1.0" xmlns:v13="http://www.momentum.co.za/crm/service/type/LanguageType/v1.0" xmlns:v14="http://www.momentum.co.za/crm/service/type/PreferredContactMethodType/v1.0" xmlns:v15="http://www.momentum.co.za/crm/service/type/CampaignType/v1.0" xmlns:v16="http://www.momentum.co.za/crm/service/type/ProductCategoryType/v1.0">
   <soapenv:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <wsse:UsernameToken>
            <wsse:Username>817221</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">1234</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <v1:CreateLeadRequest>
         <createLead>
            <v11:LeadSourceId>23627e70-a29e-e211-b8a8-005056b81ebe</v11:LeadSourceId>
            <v11:AffiliateLeadReference>852800020</v11:AffiliateLeadReference>
            <v11:Title>
               <v12:Code>852800018</v12:Code>
            </v11:Title>
            <v11:Initials>MD</v11:Initials>
            <v11:PreferredName>Marius</v11:PreferredName>
            <v11:FirstName>Marius</v11:FirstName>
            <v11:LastName>Drew</v11:LastName>
            <v11:PreferredCorrespondenceLanguage>
               <v13:Code>852800001</v13:Code>
               <v13:Description>?</v13:Description>
            </v11:PreferredCorrespondenceLanguage>
            <v11:PreferredCommunicationMethod>
               <v14:Code>852800000</v14:Code>
            </v11:PreferredCommunicationMethod>
            <v11:Campaign>
               <v15:Code>95D9042A-440E-E311-A5EB-005056B81EA5</v15:Code>
            </v11:Campaign>
            <v11:HomePhoneNumber>0723621762</v11:HomePhoneNumber>
            <v11:BusinessPhoneNumber>0723621762</v11:BusinessPhoneNumber>
            <v11:MobilePhoneNumber>0723621762</v11:MobilePhoneNumber>
            <v11:EmailAddress>mdrew@gmail.com</v11:EmailAddress>
            <v11:Notes>IMU</v11:Notes>
            <v11:ProductCategories>
               <v16:Code>d000083d-229c-e211-b8a8-005056b81ebe</v16:Code>
            </v11:ProductCategories>
         </createLead>
      </v1:CreateLeadRequest>
   </soapenv:Body>
</soapenv:Envelope>

This works perfectly on SOAPUI but it returns a soapenv:Client Internal Error on PHP.

Here's another version of the script that I've made. It still gives out an Internal Error though. Maybe there's a problem with the construction of the header that deals with the WS-Security on the web service.

<?php
$wsdl = "http://imupost.co.za/momentumcrm/CRMLeadServiceV10.wsdl";
$momurl = "https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/";
echo("Post to URL: {$momurl}\n");

$username = "derek";
$password = "Momentum1";

echo("<pre>\n");
$client = new SoapClient ($wsdl, array('location' => $momurl, 'style' => SOAP_DOCUMENT, 'trace' => true,  'soap_version' => SOAP_1_1, 'exceptions' => true, 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP));

$header='
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:UsernameToken wsu:Id="UsernameToken-45">
            <wsse:Username>'.$username.'</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
    ';

echo("<pre>\n");

$headerSoapVar = new SoapVar($header,XSD_ANYXML); 
$soapheader = new SoapHeader('wsse', "Security" , $headerSoapVar , true);
$client->__setSoapHeaders($soapheader);

$params['createLead'] = array(
        'LeadSourceId' => '23627e70-a29e-e211-b8a8-005056b81ebe',
        'AffiliateLeadReference' => '852800020',
        'Title' => array('Code' => '852800018'),
        'Initials' => 'MD',
        'PreferredName' => 'Marius',
        'FirstName' => 'Marius',
        'LastName' => 'Drew',
        'PreferredCorrespondenceLanguage' => array('Code' => '852800001'),
        'PreferredCommunicationMethod' => array('Code' =>'852800000'),
        'Campaign' => array('Code' => '95D9042A-440E-E311-A5EB-005056B81EA5'),
        'HomePhoneNumber' => '0723621762',
        'BusinessPhoneNumber' => '0723621762',
        'MobilePhoneNumber' => '0723621762',
        'EmailAddress' => 'mdrew@gmail.com',
        'Notes' => 'IMU',
        'ProductCategories' => array('Code' => 'd000083d-229c-e211-b8a8-005056b81ebe')
    ); 

try { 
    echo $result = $client->__call("createLead", array($params));
    echo "REQUEST:\n" . htmlentities($client->__getLastRequest())  . "\n";
    echo "REQUEST:\n" . $client->__getLastRequestHeaders()  . "\n";
} catch (Exception $e) { 
    $ml = new SimpleXMLElement($client->__getLastRequest());
    $ml->asXML('new.xml');
    echo "REQUEST:\n" . htmlentities($client->__getLastRequest())  . "\n";
    echo "REQUEST:\n" . $client->__getLastRequestHeaders()  . "\n";
    $msgs = $e->getMessage();
    echo "Error: $msgs"; 
} 
?>

Any suggestions? I badly needed your help. Thanks!

Community
  • 1
  • 1
maikelsabido
  • 1,253
  • 4
  • 18
  • 37
  • PHP's `SoapClient` class is definitely the way to go. Can you add/provide a link to the WSDL for the service you're trying to use? – George Brighton Sep 25 '13 at 03:44
  • Hi @GeorgeBrighton They gave me this endpoint: https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/ and they have also provided the wsdl and xsd files. you can download those here: http://sdrv.ms/16KC8o4 – maikelsabido Sep 25 '13 at 04:00
  • Please provide a catalog file for the libxml2 catalog.xml as well to have this run independent to online resources. – hakre Sep 26 '13 at 06:33
  • And why are you creating the Soap Request as a string? I tried to reproduce with your duplicate question (you now deleted it) and I got an error there telling that the WSDL has an error: Fatal error: Uncaught SoapFault exception: *" [WSDL] SOAP-ERROR: Parsing Schema: can't import schema from 'http://imupost.co.za/xsd-src/Lead_1_0.xsd'"* - I suggest you should update your question with a `SoapClient` based example with which it is possible to reproduce the problem you have. Because requesting with strings is a but cumbersome for SOAP, you won't get much feedback. – hakre Sep 27 '13 at 08:58
  • I updated the question and added another version of the script. – maikelsabido Sep 27 '13 at 09:10

2 Answers2

2

Using SoapClient, this should give you an idea of how to create the SOAP call:

try {
    $client = new SoapClient('CRMLeadServiceV10.wsdl');
    $header = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', array(
            'UsernameToken' => array(
                'Username' => 817221,
                'Password' => array(
                        '_' => 1234,
                        'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'))));
    $params = new stdClass();
    $params->createLead = new stdClass();
    $params->createLead->LeadSourceId = 1234;
    $result = $client->__soapCall('createLead', $params, null, $header);
    print_r($result);
} catch (SoapFault $e) {
    die($e->getMessage());
}
George Brighton
  • 5,131
  • 9
  • 27
  • 36
  • Thanks George. I'll try this. For the 'Title' parameter, how should I write it since it contains ? – maikelsabido Sep 25 '13 at 04:55
  • I'm getting this error: "SOAP-ERROR: Encoding: object has no 'createLead' property". I'm really wondering why. – maikelsabido Sep 25 '13 at 05:21
  • SoapClient should take care of encoding it for you. Maybe it's looking for objects - see my edit. – George Brighton Sep 25 '13 at 10:42
  • The soap:address location in WSDL is different from that makelsabido puts in the comment and even more, the WSDL location doesn't exist. If you create SoapClient proxy with this WSDL it won't work. In the other hand the location in maikelsabido's comment has transport level security with client certificate authentication (confirmed with wireshark), a valid certificate for https connection is needed to succefull https soap call. I don't know how to do this in PHP SoapClient... – jlvaquero Sep 25 '13 at 11:12
  • @jlvaquero That's the endpoint that they've provided me. The web service uses WS-Security. – maikelsabido Sep 26 '13 at 01:22
  • Upon checking the last request using $client->__getLastRequest(), the XML request is correct and if you copy the XML request and run it on SOAPUI, it gives the correct response. But, in PHP it still gives soapenv:Client Internal Error – maikelsabido Sep 26 '13 at 02:47
  • No it does not give much detail about the error. Just "Internal Error", that's why it's hard to determine the main cause of the problem. – maikelsabido Sep 27 '13 at 09:04
  • Thanks but I already tried to change the SOAP version and still doesn't work. – maikelsabido Sep 30 '13 at 02:53
0

I was able to connect to the webservice using the following code:

$client = new SoapClient ($wsdl, array('location' => $momurl, 'action'=>$action, 'style' => SOAP_DOCUMENT, 'trace' => 1,  'soap_version' => SOAP_1_1, 'exceptions' => false, 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP, 'ssl_method' => SOAP_SSL_METHOD_TLS));

$header='<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:UsernameToken wsu:Id="UsernameToken-45">
            <wsse:Username>'.$usname.'</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>';

$headerSoapVar = new SoapVar($header,XSD_ANYXML); 
$soapheader = new SoapHeader('wsse', "Security" , $headerSoapVar , true);
$client->__setSoapHeaders($soapheader);

$params['/* the function or method that you want to use */'] = array(/* insert your parameters here */);

$result = $client->__soapCall("/* the function or method that you want to use */", $params);

Check the header part of your request if it is similar to mine. If it's not then just use your own header. Basically, just copy your request's header part and just set the variables for username and password. I've tried it on another webservice and it's working fine.

maikelsabido
  • 1,253
  • 4
  • 18
  • 37