php - Soap Response into JSON Array -
i'm got api , working, in complete round way not feel comfortable with.. problem response has soap: prefix's on few indexes , cannot them work registering prefix's. way i'm band-aiding problem subtracting soap: characters @ beginning , end of string, turning json.. messy right??
perhaps didn't defining prefix's correctly soap , pur couldn't work @ all, kept saying prefix undefined.
this code
<?php ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); //variables quoting system $quote_client_id = $_get['quote_client_id']; $quote_billterms = $_get['quote_billterms']; $quote_ship_date = $_get['quote_ship_date']; //convert time iso 8601 format $quote_ship_date = (new datetime($quote_ship_date))->format('c'); //shipper variables $quote_shipper_postalcode = $_get['quote_shipper_postalcode']; $quote_shipper_country = $_get['quote_shipper_country']; $quote_shipper_state = $_get['quote_shipper_state']; $quote_shipper_city = $_get['quote_shipper_city']; //consignee variables $quote_consignee_postalcode = $_get['quote_consignee_postalcode']; $quote_consignee_country = $_get['quote_consignee_country']; $quote_consignee_state = $_get['quote_consignee_state']; $quote_consignee_city = $_get['quote_consignee_city']; //package info $quote_weight = $_get['quote_weight']; $quote_l = $_get['quote_l']; $quote_w = $_get['quote_w']; $quote_h = $_get['quote_h']; $quote_quantity = $_get['quote_quantity']; //xml request $xml = "<?xml version='1.0' encoding='utf-8'?> <soap:envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:pur='http://purolatorservices.com/'> <soap:body> <pur:getrates> <pur:request> <pur:credentials> <pur:username>username</pur:username> <pur:password>pw</pur:password> <pur:clientid>clientid</pur:clientid> </pur:credentials> <pur:ordernumber>test</pur:ordernumber> <pur:shipdate>" . $quote_ship_date . "</pur:shipdate> <pur:billingoption>" . $quote_billterms . "</pur:billingoption> <pur:shipfromaddress> <pur:city>" . $quote_shipper_city . "</pur:city> <pur:stateprov>" . $quote_shipper_state . "</pur:stateprov> <pur:postalcode>" . $quote_shipper_postalcode . "</pur:postalcode> <pur:country>" . $quote_shipper_country . "</pur:country> </pur:shipfromaddress> <pur:shiptoaddress> <pur:city>" . $quote_consignee_city . "</pur:city> <pur:stateprov>" . $quote_consignee_state . "</pur:stateprov> <pur:postalcode>" . $quote_consignee_postalcode . "</pur:postalcode> <pur:country>" . $quote_consignee_country . "</pur:country> </pur:shiptoaddress> <pur:currency>usd</pur:currency> <pur:packages> <pur:requestpackagev3> <pur:weightuom>lb</pur:weightuom> <pur:weight>" . $quote_weight . "</pur:weight> <pur:diml>" . $quote_l . "</pur:diml> <pur:dimw>" . $quote_w . "</pur:dimw> <pur:dimh>" . $quote_h . "</pur:dimh> </pur:requestpackagev3> </pur:packages> </pur:request> </pur:getrates> </soap:body> </soap:envelope> "; //xml request headers post $headers = array( "post /webservices/shippingservicesv3.asmx http/1.1", "host: sandbox.purolatorshipping.com", "content-type: text/xml; charset=utf-8", "content-length: " . strlen($xml) . "", "soapaction: http://purolatorservices.com/getrates" ); //soap url $url = "http://sandbox.purolatorshipping.com/webservices/shippingservicesv3.asmx"; //initialize curl , pull request , headers $ch = curl_init(); //pass curl options curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_postfields, $xml); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_header, false); //execute curl $response = curl_exec($ch); $header_size = curl_getinfo($ch, curlinfo_header_size); $header = substr($response, 0, $header_size); //had subtract header, , soap footers since couldn't soap name spaces working correctly ( find fix ) $body = substr($response, $header_size+18, -47); //close curl connection curl_close($ch); //activity::add("purolator api rate", "purolator api rate", "user", "1", "fa-ticket"); //load response xml , json $xml_load =simplexml_load_string($body); $array = json_decode(json_encode($xml_load) , 1); //loop through different services foreach ($array['rates']['ratev3'] $obj_key =>$row) { //shipment method echo "ship method " . $row['shipmethod'] . "<br>"; // delivery date echo "estimated delivery date " . $row['estimateddeliverydate'] . "<br>"; // estimated rate echo "rate " . $row['estimatedrate'] . "<br>"; // accessorials echo "fuel " . $row['ratedetails']['keyvaluedecimal'][4]['value'] . "<br>"; echo "<br>"; } var_dump($xml_load);
here response get
<?xml version="1.0" encoding="utf-8"?><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"><soap:body><getratesresponse xmlns="http://purolatorservices.com/"><getratesresult><status>0</status><packages /><totalshipping>0</totalshipping><rates><ratev3><shipmethod>ground</shipmethod><estimateddeliverydate>2015-07-13t05:00:00+00:00</estimateddeliverydate><estimatedrate>23.46000</estimatedrate><isavailable>true</isavailable><ratedetails><keyvaluedecimal><key>total</key><value>23.46000</value></keyvaluedecimal><keyvaluedecimal><key>tariff</key><value>19.80000</value></keyvaluedecimal><keyvaluedecimal><key>beyond</key><value>0</value></keyvaluedecimal><keyvaluedecimal><key>rural</key><value>0</value></keyvaluedecimal><keyvaluedecimal><key>fuel</key><value>3.66000</value></keyvaluedecimal><keyvaluedecimal><key>surcharges</key><value>0</value></keyvaluedecimal><keyvaluedecimal><key>insurance</key><value>0</value></keyvaluedecimal><keyvaluedecimal><key>taxes</key><value>0</value></keyvaluedecimal></ratedetails></ratev3><ratev3><shipmethod>express</shipmethod><estimateddeliverydate>2015-07-08t05:00:00+00:00</estimateddeliverydate><estimatedrate>49.41000</estimatedrate><isavailable>true</isavailable><ratedetails><keyvaluedecimal><key>total</key><value>49.41000</value></keyvaluedecimal><keyvaluedecimal><key>tariff</key><value>41.70000</value></keyvaluedecimal><keyvaluedecimal><key>beyond</key><value>0</value></keyvaluedecimal><keyvaluedecimal><key>rural</key><value>0</value></keyvaluedecimal><keyvaluedecimal><key>fuel</key><value>7.71000</value></keyvaluedecimal><keyvaluedecimal><key>surcharges</key><value>0</value></keyvaluedecimal><keyvaluedecimal><key>insurance</key><value>0</value></keyvaluedecimal><keyvaluedecimal><key>taxes</key><value>0</value></keyvaluedecimal></ratedetails></ratev3></rates></getratesresult></getratesresponse></soap:body></soap:envelope> so how take response , turn json?
thanks help, , please go easy on code :d
so how take response , turn json?
the response multiple namepspaces. namespaces concept json not support at all. turning response json sounds very bad idea, because it's - to large extend - incompatible.
to make clear: data represented within xml w/o namespaces incompatible json. forms of json serialization xml exists (jsondom, jsonml, badgerfish, jsonx - all supported php's domdocument via fluent dom), needs decision of yours tool use , data.
for example soap response, you're interested in response body, not whole soap xml. need xml parser first:
- best xml parser php
- how parse , process html/xml in php?
- fluentdom/fluentdom (and other php libraries dealing on top of domdocument)
but parser alone won't make it. need parse soap result. reference question listed above already. perhaps term namespace or xml namespaces might find more.
and soap request, suggest wrap it. example, use pre-made component soapclient it. if there wsdl. not re-invent wheel. , if there's wsdl, take if not alternative api access possible, e.g. xml or json based rest api aligned soap api.
Comments
Post a Comment