SOAP to XML conversion in PHP - php

I need to generate the following XML with SOAP:
...
<InternationalShippingServiceOption>
<ShippingService>StandardInternational</ShippingService>
<ShippingServiceCost currencyID="USD">39.99</ShippingServiceCost>
<ShippingServicePriority>1</ShippingServicePriority>
<ShipToLocation>CA</ShipToLocation>
</InternationalShippingServiceOption>
...
So I have the following SOAP array in PHP to do this:
$params = array(
'InternationalShippingServiceOption' => array(
'ShippingService'=>'StandardInternational',
'ShippingServiceCost'=>39.99,
'ShippingServicePriority'=>1,
'ShipToLocation'=>'CA',
)
)
$client = new eBaySOAP($session); //eBaySOAP extends SoapClient
$results = $client->AddItem($params);
Everything works great, except I am not generating the currencyID="USD" attribute in the ShippingServiceCost tag in the XML. How do I do this?

You don't need to use SoapVar. This works (for me at least):
$params = array(
'InternationalShippingServiceOption' => array(
'ShippingService'=>'StandardInternational',
'ShippingServiceCost' => array('_' => 39.99, 'currencyID' => 'USD')
'ShippingServicePriority'=>1,
'ShipToLocation'=>'CA',
)
)
I'm using this technique with the PayPal SOAP API.

Why, I am glad you asked. I just solved this today.
$shippingsvccostwithid = new SoapVar(array('currencyID' => $whatever),SOAP_ENC_OBJECT, 'ShippingServiceCost', 'https://your.namespace.here.com/');
$params = array("InternationalShippingServiceOption" => array(
"ShippingService" => "StandardInternational",
"ShippingServiceCost" => $shippingsvccostwithid,
"ShippingServicePriority" => 1,
"ShipToLocation" => "CA"
);
And then continue as normal.
Please let me know if you need any more help.

Related

php autoload not loading correctly?

Using php-ews I try to create a calendar event by example (just to get the hang of it):
require $server_path.'scripts/ews/vendor/autoload.php';
use garethp\ews\API;
use garethp\ews\API\Enumeration;
use garethp\ews\API\Type;
$ews = API::withUsernameAndPassword($exchange_host, $_SESSION["user_data"]["u_email"], $_SESSION["user_data"]["u_pwd"]);
Seems to work without errors.
// Start building the request.
$calendar = $ews->getCalendar();
$start = new DateTime('8:00 AM');
$end = new DateTime('9:00 AM');
$request = array(
'Items' => array(
'CalendarItem' => array(
'Start' => $start->format('c'),
'End' => $end->format('c'),
'Body' => array(
'BodyType' => Enumeration\BodyTypeType::HTML,
'_value' => 'This is <b>the</b> body'
),
'ItemClass' => Enumeration\ItemClassType::APPOINTMENT,
'Sensitivity' => Enumeration\SensitivityChoicesType::NORMAL,
'Categories' => array('Testing', 'php-ews'),
'Importance' => Enumeration\ImportanceChoicesType::NORMAL
)
),
'SendMeetingInvitations' => Enumeration\CalendarItemCreateOrDeleteOperationType::SEND_TO_NONE
);
$request = Type::buildFromArray($request);
$response = $ews->CreateItem($request);
I get:
PHP Fatal error: Call to undefined method garethp\ews\API::CreateItem() in
in the execute part ($ews->CreateItem())
Please take a look at my examples/, they cover exactly this. The firs thing to note is that creating Calendar events is incredibly simplified, so your long request isn't entirely needed. That being said, if you want to access the functions directly, you can't do
$response = $ews->CreateItem($request);
you need to do
$response = $ews->getClient()->CreateItem($request);
More info on building requests manually can be found here.

PHP - SoapClient and typemap to prevent element dissapearing

I am trying to see what is going on when SoapClient is parsing Soap response. When I call $client->__getLastResponse() I get this:
<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">
<ns0:Body>
<ns1:ListPositionBranchResponse xmlns:ns1="http://atollon.com/enterprise/wsdl">
<result>OK</result>
<time>0.00505090</time>
<ROWS>
<ROW>
<idPos>1409000</idPos>
<idBranch>122482000</idBranch>
<positionName>ekonom</positionName>
</ROW>
<ROW>
<idPos>1412000</idPos>
<idBranch>122488000</idBranch>
<positionName>IT specialista</positionName>
</ROW>
</ROWS>
</ns1:ListPositionBranchResponse>
</ns0:Body>
</ns0:Envelope>
I create SoapClient this way:
$client = new SoapClient("https://mblue.atollon.com/recruitment.wsdl", array('trace' => 1, 'exceptions'=>true, 'location' => "https://mblue.atollon.com/soap"));
$requestParams = array(
'server' => "SERVER",
'session' => "SESSION"
);
$response = $client->ListPositionBranch($requestParams);
but when I do var_dump of response, there is no idPos element. I have tried it on 64bit Linux machine, but it doesn't work also, so it's not INT limitation. So I wanted to see what is going on, and tried this:
$client = new SoapClient("https://mblue.atollon.com/recruitment.wsdl", array('trace' => 1, 'exceptions'=>true, 'location' => "https://mblue.atollon.com/soap", 'typemap' => array(
array(
'type_ns' => 'http://atollon.com/enterprise/wsdl',
'type_name' => 'idPos',
'to_xml' => 'to_long_xml',
'from_xml' => 'from_long_xml',
)
)));
And the functions themselves:
function to_long_xml($longVal) {
return '<long>' . $longVal . '</long>';
}
function from_long_xml($xmlFragmentString) {
return (string)strip_tags($xmlFragmentString);
}
But the from_long_xml function is not even fired.. I tried to change type_name to I guess all elements in that response, but it never fires it. I am out of ideas - what can be wrong - I am guessing it might be the namespace, but I have tried both listed in xml with no luck. How to get that typemap (or the idPos) working?
Thanks.
type_name in typemap element should be name of type defined in WSDL of service. I had weird experience, that typemap should be defined before classmap element.

How to request an answer from a SOAP XML API?

I never worked with SOAP XML api before.
I read a couple of similar questions on SO but I can't get it to work.
Here's a simple request:
<soap12:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap12=”http://www.w3.org/2003/05/
soap-envelope”>
<soap12:Body>
<CheckDomainAvailability xmlns=”https://live.domainbox.net/”>
<AuthenticationParameters>
<Reseller>myreseller</Reseller>
<Username>myuser</Username>
<Password>mypassword</Password>
</AuthenticationParameters>
<CommandParameters>
<DomainName>checkadomain.co</DomainName>
<LaunchPhase>GA</LaunchPhase>
</CommandParameters>
</CheckDomainAvailability>
</soap12:Body>
</soap12:Envelope>
I've contacted them but they do not offer a PHP API.
I would like to use the SoapClient class built in PHP.
The question is:
How do I send the request and print the answer?
Looks like your WSDL is located at https://live.domainbox.net/?WSDL.
Here's an example using the native PHP SoapClient.
$client = new SoapClient('https://live.domainbox.net/?WSDL');
// populate the inputs....
$params = array(
'AuthenticationParameters' => array(
'Reseller' => '',
'Username' => '',
'Password' => ''
),
'CommandParameters' => array(
'DomainName' => '',
'LaunchPhase' => ''
)
);
$result = $client->CheckDomainAvailability($params);
print_r($result);
To get this working I needed to change the first line to get the right version of soap to 1.2.
Also a previous comment
$client = new SoapClient('https://live.domainbox.net/?WSDL', array('soap_version' => SOAP_1_2));
// populate the inputs....
$params = array(
'AuthenticationParameters' => array(
'Reseller' => '',
'Username' => '',
'Password' => ''
),
'CommandParameters' => array(
'DomainName' => '',
'LaunchPhase' => ''
)
);
$result = $client->CheckDomainAvailability($params);
print_r($result);

Search google+ api for public posts and pages

I'm trying to use the google-api-php-client SDK along with google plus api service. The documentation isnt very clear. Do I search activities? Do I need to get an oAuth token to do what i want?
Update: Now using
$params = array(
'orderBy' => 'best',
'maxResults' => '20',
'query' => 'Google+ API'
);
$results = $plus->activities->search($params);
returns
Array
(
[kind] => plus#activityFeed
[etag] => "5NTCbsXue5u92XtxuV0QeM_x9B4/58mHUy-IErItGIABIAhYhhUjJ-M"
[selfLink] => https://www.googleapis.com/plus/v1/activities?query&maxResults=10
[title] => Plus Search
[updated] => 2012-09-27T14:03:35.132Z
[id] => tag:google.com,2010:es-search-feed:z80ghJXVZe9m59_5-Ydk2g
[items] => Array
(
)
)
I had a same problem and here is how I have done it:
$query = "some query";
$params = array(
'orderBy' => 'best',
'maxResults' => '20',
);
$results = $plus->activities->search($query, $params);
foreach($results['items'] as $result) {
print "Result: {$result['object']['content']}\n";
}
Even dough in an examples query is a part of parameters it is not. It should be sent as separated value as you can see in the implementation of the search method for plus service Link
Hope this helps you.
You don't need an OAuth token to search activities. You only need to register a Developer Key in the Google APIs Console.
Register a Developer Key in the Google APIs Console.
Check out the latest version of the Google APIs PHP Client .
Take a look at the Google+ sample app. https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/plus/index.php
Sample:
$params = array(
'orderBy' => 'best',
'maxResults' => '20',
'query' => 'Google+ API'
);
$results = $plus->activities->search($params);
foreach($results['items'] as $result) {
print "Result: {$result['object']['content']}\n";
}

SoapClient throws Wrong version

When i sending below request, i am getting 'Wrong Version" exception.
<OTA_HotelGetMsgRQ xmlns="http://www.opentravel.org/OTA/2003/05"
TimeStamp="2001-12-17T09:30:47.0Z" Version="4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Messages>
<Message HotelCode="123" HotelName="Test Hotel" ChainCode="321"
ReasonForRequest="Reservation Retrieval" RequestCode="Optional"
ChainName="Test Chain" MessageType="All" StartSeqNmbr="1"
EndSeqNmbr="10" />
</Messages>
</OTA_HotelGetMsgRQ>
above request is converted into zend code
$client = new zend_soap_client(null,
array(
'location' => 'http://url...',
'Uri' =>"http://www.opentravel.org/OTA/2003/05"
)
);
$request = array(
array('Messages'=>
array ('Message' =>
array (
'HotelCode' => '123',
'HotelName' => 'Test Hotel',
'ChainCode' => '321',
'ReasonForRequest' => 'Reservation Retrieval',
'RequestCode' => 'Optional',
'ChainName' => 'Test Chain',
'MessageType' => 'All',
'StartSeqNmbr' => '1',
'EndSeqNmbr' => '10'
)
)
)
);
$result = $client->OTA_HotelGetMsgRQ ($request);
Above line throws exception 'Wrong Version'. Anyone help me how to solve this
sounds like you are using the wrong version ;) - have you tried to add 'soap_version'=>'1.2' (or 1.1, depending on which version is needed) to the options of your soap_client?
$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('Core_Soap_Test');
$autodiscover->setUri( sprintf($this->_WSDL_URI,0) );
$autodiscover->handle();
$this->_WSDL_URI = '"http://WWW.EXAMPLE.COM/soap/index/wsdl/%s/?wsdl'
try the code above. this helped me
I had this problem as well. The problem in my case was that I wasn't passing an expected (and thus required) parameter without throwing back a SoapFault-exception. Maybe it's useful to someone.

Categories