How can I create php soap request to look like this. I am unable to get the response in Php curl. But fiddler web debugger is working really well with the code.
Here is the raw request:
POST http://195.230.180.189:8280/services/TopupService?wsdl HTTP/0.9
Host: 195.230.180.189:8280
Content-Length: 691
<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:top="http://www.inew-cs.com/mvno/integration/TopupService/">
<soapenv:Header/>
<soapenv:Body>
<top:TopupRequest>
<amount>1</amount>
<amountUnitRelation>1</amountUnitRelation>
<subscriber>
<msisdn>8801701340002</msisdn>
</subscriber>
<source>
<distributorId>PayWell</distributorId>
</source>
<referenceId>12345</referenceId>
<transactionId>09876543</transactionId>
</top:TopupRequest>
</soapenv:Body>
</soapenv:Envelope>
In Php curl request:
$url="http://195.230.180.189:8280/services/TopupService?wsdl";
$xml='<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:top="http://www.inew-cs.com/mvno/integration/TopupService/">
<soapenv:Header/>
<soapenv:Body>
<top:TopupRequest>
<amount>1</amount>
<amountUnitRelation>1</amountUnitRelation>
<subscriber>
<msisdn>8801701340002</msisdn>
</subscriber>
<source>
<distributorId>100</distributorId>
</source>
<referenceId>12345</referenceId>
<transactionId>09876543</transactionId>
</top:TopupRequest>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
echo $result = curl_exec($ch);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
//echo 'Operation completed without any errors';
}
// Close handle
curl_close($ch);
You should not post to that URL. That is not a service endpoint, it is the WSDL, which defines the provided operations.
PHP SoapClient class allows you to build soap requests easily:
$wsdl = 'http://195.230.180.189:8280/services/TopupService?wsdl';
$options = [
'trace' => true,
'cache' => WSDL_CACHE_NONE,
'exceptions' => true
];
$client = new SoapClient($wsdl, $options);
$payload = [
'amount' => 1,
'amountUnitRelation' => 1,
'subscriber' => [
'msisdn' => '8801701340002'
],
'source' => [
'distributorId' => 'PayWell'
],
'referenceId' => '12345',
'transactionId' => '09876543',
];
$response = $client->topup($payload);
After parsing the given wsdl, the $client now has the method topup, as defined by <wsdl:operation>.
Related
I am trying to execute following WSDL request in PHP. SOAP api does not have any authentication.
When i am running this code using SOAP UI it is working fine. But through code it is not working.
Here is my code:
<?php
$soapUrl = "https://dimasys.plasticentre.be:8443/dimasys/prices/DimasysService.wsdl";
// xml post structure
$xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins="http://www.infomat.eu/dimasys/insertorderrequest"><soapenv:Header/><soapenv:Body><ins:insertOrderRequest><ins:order><ins:order_no>TEST-DODE 8</ins:order_no><ins:customer><ins:customer_no></ins:customer_no><ins:besteladresnummer>1</ins:besteladresnummer><ins:customer_user_volgnr>0</ins:customer_user_volgnr></ins:customer><ins:email>helpdima#infomat.eu</ins:email><ins:created_dt>2021-02-05</ins:created_dt><ins:firma>PLC</ins:firma><ins:billingAddress><ins:address><ins:company>Test infomat</ins:company><ins:initials></ins:initials><ins:firstname></ins:firstname><ins:prefix></ins:prefix><ins:lastname></ins:lastname><ins:addressline1>Laarstraat</ins:addressline1><ins:addressline2></ins:addressline2><ins:house_number>16</ins:house_number><ins:house_number_addition></ins:house_number_addition><ins:zipcode>2610</ins:zipcode><ins:city>Wilrijk</ins:city><ins:state></ins:state><ins:country><ins:countrycode>BE</ins:countrycode><ins:name>Belgiƫ</ins:name></ins:country><ins:addressnumber></ins:addressnumber></ins:address></ins:billingAddress><ins:shippingAddress><ins:address><ins:company>Test infomat</ins:company><ins:initials></ins:initials><ins:firstname></ins:firstname><ins:prefix></ins:prefix><ins:lastname></ins:lastname><ins:addressline1>Laarstraat</ins:addressline1><ins:addressline2></ins:addressline2><ins:house_number>16</ins:house_number><ins:house_number_addition></ins:house_number_addition><ins:zipcode>2610</ins:zipcode><ins:city>Wilrijk</ins:city><ins:state></ins:state><ins:country><ins:countrycode>BE</ins:countrycode><ins:name>Belgiƫ</ins:name></ins:country><ins:addressnumber></ins:addressnumber></ins:address></ins:shippingAddress><ins:orderLines><ins:order_line><ins:line_id>10</ins:line_id><ins:sku>929740</ins:sku><ins:eancode></ins:eancode><ins:description></ins:description><ins:qty_ordered>2</ins:qty_ordered><ins:regular_price></ins:regular_price><ins:promo_price></ins:promo_price><ins:discount_percentage></ins:discount_percentage><ins:discount_amount></ins:discount_amount><ins:sales_price></ins:sales_price><ins:total_amount></ins:total_amount><ins:vat_rate></ins:vat_rate></ins:order_line></ins:orderLines><ins:shippingCosts>0</ins:shippingCosts><ins:subTotal></ins:subTotal><ins:shippingMethod></ins:shippingMethod><ins:paymentMethod></ins:paymentMethod><ins:paymentReference></ins:paymentReference></ins:order></ins:insertOrderRequest></soapenv:Body></soapenv:Envelope>'; // data from the form, e.g. some ID number
$webService = new SoapClient($soapUrl);
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: https://dimasys.plasticentre.be:8443/dimasys/insertorders",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
echo "<pre>";var_Dump($response);exit;
?>
I am getting blank response in return. Please help me how i can do it.
I tried SOAP Client as well as php CURL.
Using SoapClient I'm able to connect to the SOAPService when I'm setting the location parameter. This parameter is useful whenever the SOAPService sits behind a proxy and answers with its local IP address. When omitting this parameter I cant connect to the server.
Note that I'm sending dummy data to the Server which results in a SoapFault Server Error.
The created request from the wsdl is sent inside the parameters array.
Hope this is useful as a starting point.
<?php
try {
$wsdlUrl = 'https://dimasys.plasticentre.be:8443/dimasys/prices/DimasysService.wsdl';
$soapOptions = [
'soap_version' => SOAP_1_2,
'location' => 'https://dimasys.plasticentre.be:8443/dimasys/prices/DimasysService.wsdl',
'encoding' => 'utf-8',
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE
];
$client = new SoapClient($wsdlUrl, $soapOptions);
$return = $client->__soapCall('getPrijzen', [
'parameters' => [
'tarieflijst' => 'test',
'factuuradresnummer' => 10,
'leveradresnummer' => 10,
'taal' => 'test',
'muntcode' => 'test',
'artikelen' => [
'artikel' => [
'artikelcode' => '123',
'aantal' => 10
]
]
]
]);
var_dump($return);
} catch(Exception $e) {
var_dump($e);
}
I'm trying to consume this webservice with php.
First I need to call the API method to make login.
Here's my code:
try {
$opts = array(
'http' => array(
'user_agent' => 'PHPSoapClient'
)
);
$context = stream_context_create($opts);
$wsdlUrl = 'http://172.20.2.18:1024/ADInterface/services/ModelADService?wsdl';
$soapClientOptions = array(
'stream_context' => $context,
'cache_wsdl' => WSDL_CACHE_NONE);
$checkVatParameters = array(
'user'=>'WebService',
'pass'=>'WebService',
'lang'=>'es_CL',
'ClientID'=>'1000000',
'RoleID'=>'1000014',
'OrgID'=>'1000000',
'WarehouseID'=>'1000001',
'stage'=>'0');
$modelCrud = array(
'serviceType' => 'WSBPartner',
'TableName' => 'XX_WEB_WSBPartner',
'RecordID' => 0,
'Filter' => '',
'Action' => 'Read',
'DataRow' => array(
'field' => array(
'type' => 'integer',
'column' => 'C_BPartner_ID',
'lval' => '',
'disp' => '',
'edit' => '',
'error' => '',
'errorVal' => '',
'val' => 1000643,
)
)
);
$client = new SoapClient($wsdlUrl, $soapClientOptions);
$result = $client->queryData(
'ModelCRUDRequest', array(
'ModelCRUD' => $modelCrud,
'ADLoginRequest' => $checkVatParameters,
)
);
print_r($result);
}
catch(Exception $e) {
echo $e->getMessage();
}
here's the error: Parameter ModelCRUDRequest does not exist!
I want to be able to call those method like queryData. I hope I explained well, this is the first time I'm using webservices.
Sometimes the SoapClient is just a "broken tool". I would try to use curl or Guzzle as HTTP client and build the SOAP request manually. I have used SoapUI to generate a sample SOAP request according to the WSDL.
Example
<?php
// Change the url
$endpoint = 'http://172.20.2.18:1024/ADInterface/services/ModelADService';
$soapMethod = 'queryData';
// Basic Auth (optional)
$soapUser = ''.
$soapPassword = '';
// Created with SoapUI
$soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:adin="http://3e.pl/ADInterface">
<soapenv:Header/>
<soapenv:Body>
<adin:queryData>
<adin:ModelCRUDRequest>
<adin:ModelCRUD>
<adin:serviceType>?</adin:serviceType>
<adin:TableName>?</adin:TableName>
<adin:RecordID>?</adin:RecordID>
<adin:Filter>?</adin:Filter>
<adin:Action>?</adin:Action>
<!--Optional:-->
<adin:DataRow>
<!--Zero or more repetitions:-->
<adin:field type="?" column="?" lval="?" disp="?" edit="?" error="?" errorVal="?">
<adin:val>?</adin:val>
<!--Optional:-->
<adin:lookup>
<!--Zero or more repetitions:-->
<adin:lv val="?" key="?"/>
</adin:lookup>
</adin:field>
</adin:DataRow>
</adin:ModelCRUD>
<adin:ADLoginRequest>
<adin:user>?</adin:user>
<adin:pass>?</adin:pass>
<adin:lang>?</adin:lang>
<adin:ClientID>?</adin:ClientID>
<adin:RoleID>?</adin:RoleID>
<adin:OrgID>?</adin:OrgID>
<adin:WarehouseID>?</adin:WarehouseID>
<adin:stage>?</adin:stage>
</adin:ADLoginRequest>
</adin:ModelCRUDRequest>
</adin:queryData>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
'Content-type: text/xml;charset="utf-8"',
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
// Maybe change this url
'SOAPAction: ' . $endpoint . '/' . $soapMethod,
'Content-length: ' .strlen($soap),
);
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
// Basic Auth (optional)
curl_setopt($ch, CURLOPT_USERPWD, $soapUser. ':' .$soapPassword);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $soap);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Invoke request
$response = curl_exec($ch);
if($response === false) {
echo 'HTTP error: ' . curl_error($ch);
exit;
}
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$xml = trim(substr($response, $headerSize));
curl_close($ch);
// Convert response to DOM document
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml);
echo $dom->saveXML();
I am trying to make a SOAP request using cURL but I am getting the following errors. For the output of the result, I get "bool(false)". For the curl_error() function, I get "Operation timed out after 30000 milliseconds with 0 bytes received".
My question is whether I am missing something that could be preventing the connection, possibly in the cURL options?
This is my code:
<?php
function soap_subscriber($subscriber_email) {
$soap_endpoint = '[HIDDEN]';
$soap_username = '[HIDDEN]';
$soap_userpass = '[HIDDEN]';
define('USERNAME', '[HIDDEN]');
define('PASSWORD', '[HIDDEN]');
define('ENDPOINT', $soap_endpoint);
date_default_timezone_set('America/Nassau');
$ch = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
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">
<soap:Header>
<wsse:Security soap:mustUnderstand="1">
<wsse:UsernameToken wsu:Id="SecurityToken-884da619-59bb-4db6-834d-138322342442">
<wsse:Username> . soap_username . </wsse:Username>
<wsse:Password
Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"
> . soap_userpass . </wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body>
<RetrieveRequestMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">
<RetrieveRequest>
<ClientIDs>
<ID>[HIDDEN]</ID>
</ClientIDs>
<ObjectType>DataExtensionObject[HIDDEN]</ObjectType>
<Properties>SubscriberID</Properties>
<Properties>FirstName</Properties>
<Properties>Email</Properties>
<Filter xmlns:q1="http://exacttarget.com/wsdl/partnerAPI" xsi:type="q1:SimpleFilterPart">
<q1:Property>Email</q1:Property>
<q1:SimpleOperator>equals</q1:SimpleOperator>
<q1:Value> . $subscriber_email . </q1:Value>
</Filter>
</RetrieveRequest>
</RetrieveRequestMsg>
</soap:Body>
</soap:Envelope>';
$soap_header = array(
"Content-type: application/soap+xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: ",
"Content-length: ".strlen($ch),
);
$ch = curl_init();
$curl_options = array(
CURLOPT_URL => ENDPOINT,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERPWD => USERNAME . ':' . PASSWORD,
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HEADER => true,
CURLOPT_POSTFIELDS => $ch,
CURLOPT_HTTPHEADER => $soap_header,
CURLOPT_POST => true,
);
curl_setopt_array($ch, $curl_options);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
var_dump($response);
var_dump($error);
}
soap_subscriber('[HIDDEN]');
?>
Your SOAP request requires more than 30 seconds to finish its execution. Some solutions you can try:
Increase cURL operation time limit:
curl_setopt($ch, CURLOPT_TIMEOUT, 400); // 400 seconds
Set set_time_limit to 0 to make your requests run for infinite seconds:
set_time_limit(0);
but downside is that hanging or running out of memory is possible.
Try adding this to your cURL options to prevent infinite redirection from the server:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
Hope this helps!
I'm using SoapUI 5.3.0 to test a SOAP ws request.
I was requested to send user and password via https headers not via soap headers.
It works well when I use this SoapUi tool:
However, when I try to do it from php I always get an authentication error, exactly the same error I get when I use a wrong password on purpose, I've tried several combinations but none of them gave me expected results
code example:
$data['Contrato'] = '123456';
$data['FechaInicio'] = '11/07/2017';
$data['FechaFin'] ='11/07/2017';
$client = new SoapClient( "https://example.com/WebService?WSDL", array(
"exceptions" => 0,
"trace" => 1,
'stream_context' => stream_context_create(array(
'http' => array(
'header' => 'Username:xxx#gmail.com\\n\\r Password:notrealpwd'
),
)),
));
$result = $client->__soapCall('depositos', $data);
Does any of you knows what I am doing wrong?
try with:
$client = new SoapClient($wsdl, array("trace" => 1, "exceptions" => 0,
"login" => $login, "password" => $password) );
At the end I finished using curl to set the needed headers, that solved it.
<?php
// xml post structure
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hub="https://example.com/WebService?WSDL">
<soapenv:Header/>
<soapenv:Body>
<hub:depositos>
<!--Optional:-->
<hub:solicitud>
<Contrato>123456</Contrato>
<FechaInicio>11/07/2017</FechaInicio>
<!--Optional:-->
<FechaFin>11/07/2017</FechaFin>
</hub:solicitud>
</hub:depositos>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: ''",
"Content-length: ".strlen($xml_post_string),
"Username: xxx#gmail.com",
"Password: notrealpwd"
);
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $ws_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// converting
$response = curl_exec($ch);
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!
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());
}
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.