$curl = curl_init();
$xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Auth uid=”999999990019” tid=”public” ac=”public” sa=”public” ver=”1.6”txn=”AuthDemoClient:public:20160616070338756” lk=”MBFWjkJHNFfLidl8oOHtUwgL5p1ZjDbWrqsMEVEJLVEDpnlNj_CZTg”>
<Uses pi=”y” pa=”n” pfa=”n” bio=”n” bt=”n” pin=”” otp=”n”/>
<Tkn type=”” value=””/>
<Meta udc=”” fdc=”” idc=”” pip=”” lot=”G|P” lov=””/>
<Skey ci=”” ki=””></Skey>
<Data type=”X|P”>encrypted PID block</Data>
<Hmac>SHA-256 Hash of Pid block, encrypted and then encoded</Hmac
<Signature>Digital signature of AUA</Signature>
</Auth>
</xml>';
curl_setopt($curl, CURLOPT_URL,"http://auth.uidai.gov.in/1.6/public/9/9/MH4hSkrev2h_Feu0lBRC8NI-iqzT299_qPSSstOFbNFTwWrie29ThDo");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml','Content-Length:length'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml_data);
$result = curl_exec($curl);
curl_close($curl);
print_r($result);
This is the sample code which I wrote.Needs test data for some of the left out attributes like udc,fdc,idc etc. When I try to fire this API gets 400-Bad Request.Help would be appriciated.
Related
I'm using the following code to send an request to a SOAP service:
header('Content-type: application/xml');
function doXMLCurl($url,$postXML){
$CURL = curl_init();
curl_setopt($CURL, CURLOPT_URL, $url);
curl_setopt($CURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($CURL, CURLOPT_POST, 1);
curl_setopt($CURL, CURLOPT_POSTFIELDS, $postXML);
curl_setopt($CURL, CURLOPT_HEADER, false);
curl_setopt($CURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($CURL, CURLOPT_HTTPHEADER, array('Accept: text/xml','Content-Type: application/soap+xml'));
curl_setopt($CURL, CURLOPT_RETURNTRANSFER, true);
$xmlResponse = curl_exec($CURL);
return $xmlResponse;
}
$input_xml = '<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">.....</s:Envelope>';
echo doXMLCurl('webservice_url', $input_xml);
The responde is a XML too.
How can i parse this data or convert to an array or object? I tried with simplexml_load_string() but without success.
EDIT
XML Response:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">...</a:Action>
</s:Header>
<s:Body>
<ListStatesResponse xmlns="http://tempuri.org/">
<ListStatesResult xmlns:b="..." xmlns:i="...">
<b:Return>0</b:Return>
<b:Message i:nil="true"/>
<b:Status>00</b:Status>
<b:ListStates>
<b:States>
<b:Description>ACRE</b:Description>
<b:Code>AC</b:Code>
</b:States>
<b:States>
<b:Description>ALAGOAS</b:Description>
<b:Code>AL</b:Code>
</b:States>
<b:States>
<b:Description>AMAZONAS</b:Description>
<b:Code>AM</b:Code>
</b:States>
...
</b:ListStates>
</ListStatesResult>
</ListStatesResponse>
</s:Body>
</s:Envelope>
Assuming the xml is valid, you can use SimpleXMLElement, i.e.:
$xml = new SimpleXMLElement($xmlResponse);
echo $xml->node->otherNode['Id'];
To loop it, use:
foreach ($xml->node->otherNode as $el) {
foreach($el as $key => $val) {
echo "{$key}: {$val}";
}
}
I found the easiest way is to use json functions
$jsonObject = json_decode(json_encode($xmlString));
Then print_r($jsonObject) to find the structure. This allows attributes to be accessible to using $jsonObject->{'#attributes'}->id;
I have a code here and can get only current oil price but I need to get a result the same as the picture below. I have no idea to do this.
<?php
$soapUrl = "http://www.pttplc.com/webservice/pttinfo.asmx?op=CurrentOilPrice";
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<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>
<CurrentOilPrice xmlns="http://www.pttplc.com/ptt_webservice/">
<Language>string</Language>
</CurrentOilPrice>
</soap12:Body>
</soap12:Envelope>';
$headers = array(
"POST /webservice/pttinfo.asmx HTTP/1.1",
"Host: www.pttplc.com",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: " . strlen($xml_post_string)
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response1 = str_replace("<soap:Body>", "", $response);
$response2 = str_replace("</soap:Body>", "", $response1);
$parser = simplexml_load_string($response2);
echo $parser->asXML();
You can use SOAP CLIENT is a good tool for use if you work with Web services. This is easy to use, automaticaly convert your response in SimpleXMLObject and you are able to use XPath to find easy your XML tag.
When I try to POST this XML document to Blackboard Learn LIS using PHP cURL I get this error. I'm checking the XML with simplexml to ensure it is well formed, so that's not the issue. I have also checked the XML document to ensure there is no BOM data attached. I've opened it in a Hex editor to ensure this.
I'm completely stumped on this one and I don't have access to the Blackboard logs.
PHP:
include("xml/envelope.php");
//sanity check to see if xml is well formed
$sxml = simplexml_load_string($xml);
$xml = $sxml->asXML();
$xml_length = strlen($xml);
$url = $this->lis_outcome_service_url;
$bodyHash = base64_encode(sha1($xml, TRUE)); // build oauth_body_hash
$consumer = new OAuthConsumer($key, $secret);
$request = OAuthRequest::from_consumer_and_token($consumer, '', 'POST', $url, array('oauth_body_hash' => $bodyHash) );
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, '');
$header = $request->to_header() . "Content-Type: application/xml\r\nContent-Length: $xml_length\r\n"; // add content type header
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
//curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$output .= "$url<br><br>$output<pre>$header\n\n".htmlspecialchars($xml)."</pre><p>ERRORS?: ".curl_error($ch);
curl_close($ch);// "<p>Saved grade: "+var_export($output)+"</p>";
return $output;
The XML in envelope.php:
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<imsx_POXEnvelopeRequest xmlns="http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0">
<imsx_POXHeader>
<imsx_POXRequestHeaderInfo>
<imsx_version>V1.0</imsx_version>
<imsx_messageIdentifier>$id</imsx_messageIdentifier>
</imsx_POXRequestHeaderInfo>
</imsx_POXHeader>
<imsx_POXBody>
<replaceResultRequest>
<resultRecord>
<sourcedGUID>
<sourcedId>$this->lis_result_sourcedid</sourcedId>
</sourcedGUID>
<result>
<resultScore>
<language>en-us</language>
<textString>0.7</textString>
</resultScore>
</result>
</resultRecord>
</replaceResultRequest>
</imsx_POXBody>
</imsx_POXEnvelopeRequest>
XML;
?>
The XML output in browser after parsing (during cURL request):
<?xml version="1.0" encoding="UTF-8"?>
<imsx_POXEnvelopeRequest xmlns="http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0">
<imsx_POXHeader>
<imsx_POXRequestHeaderInfo>
<imsx_version>V1.0</imsx_version>
<imsx_messageIdentifier>53e4657ec1499</imsx_messageIdentifier>
</imsx_POXRequestHeaderInfo>
</imsx_POXHeader>
<imsx_POXBody>
<replaceResultRequest>
<resultRecord>
<sourcedGUID>
<sourcedId>bbgc15659375gi290156</sourcedId>
</sourcedGUID>
<result>
<resultScore>
<language>en-us</language>
<textString>0.7</textString>
</resultScore>
</result>
</resultRecord>
</replaceResultRequest>
</imsx_POXBody>
</imsx_POXEnvelopeRequest>
Since there is so little detailed information on how to do this in LTI, I've posted my solution below. This is a duplicate of my addition to Blackboard's EduGarage forums, but to get it into the public domain I've put it here. I hope it helps other people trying to implement LTI tools.
Here is how to pass back a grade using PHP cURL, the contents of the envelope.php file shouldfollow the XML found in the IMS Global Documentation found here. The cURL request constructs the header and then adds the XML body below it.
Note that the envelope.php file contains the $xml variable in the following format (you will obviously need to add your own $id and set a grade with a variable etc...):
PHP code for passing grade using cURL:
include("xml/envelope.php");
$xml_length = strlen($xml);
$url = $this->lis_outcome_service_url;
$bodyHash = base64_encode(sha1($xml, TRUE)); // build oauth_body_hash
$consumer = new OAuthConsumer($key, $secret);
$request = OAuthRequest::from_consumer_and_token($consumer, '', 'POST', $url, array('oauth_body_hash' => $bodyHash) );
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, '');
$header .= $request->to_header(); // add content type header
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("POST http://your-tools-url-here.edu.au HTTP/1.0",
"Content-Length: $xml_length", $header, "Content-Type: application/xml"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
I'm using Google's own example from https://developers.google.com/google-apps/spreadsheets/
They say:
To update the contents of an existing row, first retrieve the row to update, modify it as desired, and then send a PUT request, with the updated row in the message body, to the row's edit URL.
Be sure that the id value in the entry you PUT exactly matches the id of the existing entry. The edit URL is highlighted in the following row entry:
<entry gd:etag='"S0wCTlpIIip7ImA0X0QI"'>
<id>https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId</id>
<updated>2006-11-17T18:23:45.173Z</updated>
<category scheme="http://schemas.google.com/spreadsheets/2006"
term="http://schemas.google.com/spreadsheets/2006#list"/>
<title type="text">Bingley</title>
<content type="text">Hours: 10, Items: 2, IPM: 0.0033</content>
<link rel="self" type="application/atom+xml"
href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId"/>
<link rel="edit" type="application/atom+xml"
href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId/version"/>
<gsx:name>Bingley</gsx:name>
<gsx:hours>20</gsx:hours>
<gsx:items>4</gsx:items>
<gsx:ipm>0.0033</gsx:ipm>
</entry>
I've been attempting to send this info (using my own data, but exact same structure) with the following CURL:
$headers = array(
"Authorization: GoogleLogin auth=" . $google_auth,
"GData-Version: 3.0",
"Content-Type: application/atom+xml",
"If-Match: *",
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/feeds/list/$feed/0/private/full/$row_id");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = curl_exec($curl);
echo $response;
It seems that the XML is malformed which is odd as it comes right from Google's example and I get the same response when retrieving a list feed from my own spreadsheets. How do I correct and are there any other issues?
When I paste this xml into http://www.freeformatter.com/xml-formatter.html, I receive an error that
Unable to parse any XML input. org.jdom2.input.JDOMParseException: Error on line 1: The prefix "gd" for attribute "gd:etag" associated with an element type "entry" is not bound.
Thanks.
Well, no real responses....but I finally got it working. Hopefully this helps someone else. The idea is you need to use the namespaces which are retrieved with the feed.
So the XML should look like:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" gd:etag=""S0wCTlpIIip7ImA0X0QI"">
<id>https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId</id>
<updated>2006-11-17T18:23:45.173Z</updated>
<category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#list" />
<title type="text">Bingley</title>
<content type="text">Hours: 10, Items: 2, IPM: 0.0033</content>
<link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId" />
<link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId/version" />
<gsx:name>Bingley</gsx:name>
<gsx:hours>20</gsx:hours>
<gsx:items>4</gsx:items>
<gsx:ipm>0.0033</gsx:ipm>
</entry>
And the CURL should look like:
$headers = array(
"Authorization: GoogleLogin auth=" . $google_auth, //google_auth retrieved earlier
"GData-Version: 3.0",
"Content-Type: application/atom+xml",
"If-Match: *",
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/feeds/list/$feed/0/private/full/$id"); //feed is obtained from spreadsheet url and id can be obtained by retrieving list feed
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = curl_exec($curl);
echo $response;
If you want to retrieve and update a proper custom XML instead of a generic Atom XML doc, then you can try APISpark PaaS which has a wrapper for GSpreadsheets that can create and host a custom web API.
See tutorial: https://apispark.com/docs/tutorials/google-spreadsheet
This should be help to you
$accessToken = $ACCESS_TOKEN;
$key = $SPREADSHEET_ID;
$sheetId = $SHEET_ID;
$editUrl = "https://spreadsheets.google.com/feeds/cells/$key/$sheetId/private/full/R2C4?access_token=$accessToken";
$entry = '<?xml version="1.0" encoding="UTF-8" ?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006"><id>https://spreadsheets.google.com/feeds/cells/'.$key.'/'.$sheetId.'/private/full/R2C4</id><link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/'.$key.'/'.$sheetId.'/private/full/R2C4"/><gs:cell row="2" col="4" inputValue="Enter_Value_Here">Enter_Value_Here</gs:cell></entry>';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $editUrl );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml','If-Match: *','Content-length: ' . strlen($entry)));
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $entry );
$content = curl_exec($ch);
if($content === false)
{
echo 'Curl error: ' . curl_error($ch)."<br>";
}
else
{
echo 'Operation completed without any errors <br>';
$information = curl_getinfo($ch);
}
curl_close($ch);
In my website(php based) I want implement a rate calculator of DHL. I want to add 3 fields-1.Origin, 2.Destination and 3.Weight. These 3 values will be sent to DHL server and in return I want to have the RATE. How can I do that?
In another section, I will add more field (address, product hts code, etc.) with those 3 to get the RATE. How can it be done also??
Below is DHL rate calculator code: You need to change siteid and password with your DHL siteid and password.
<?php
$data = '<?xml version="1.0" encoding="UTF-8"?>
<p:DCTRequest xmlns:p="http://www.dhl.com" xmlns:p1="http://www.dhl.com/datatypes" xmlns:p2="http://www.dhl.com/DCTRequestdatatypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com DCT-req.xsd ">
<GetQuote>
<Request>
<ServiceHeader>
<MessageTime>'.date('c').'</MessageTime>
<MessageReference>1234567890123456789012345678901</MessageReference>
<SiteID>YOUR_DHL_SITE_ID</SiteID>
<Password>YOUR_DHL_PASSWORD</Password>
</ServiceHeader>
</Request>
<From>
<CountryCode>GB</CountryCode>
<Postalcode>WC1A</Postalcode>
</From>
<BkgDetails>
<PaymentCountryCode>US</PaymentCountryCode>
<Date>2011-06-06</Date>
<ReadyTime>PT10H21M</ReadyTime>
<ReadyTimeGMTOffset>+01:00</ReadyTimeGMTOffset>
<DimensionUnit>CM</DimensionUnit>
<WeightUnit>KG</WeightUnit>
<Pieces><Piece>
<PieceID>1</PieceID>
<Height>20</Height>
<Depth>20</Depth>
<Width>20</Width>
<Weight>19</Weight>
</Piece></Pieces>
<IsDutiable>N</IsDutiable>
<NetworkTypeCode>AL</NetworkTypeCode>
</BkgDetails>
<To>
<CountryCode>US</CountryCode>
<Postalcode>10101</Postalcode>
</To>
</GetQuote>
</p:DCTRequest>';
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://xmlpitest-ea.dhl.com/XMLShippingServlet");
curl_setopt($tuCurl, CURLOPT_PORT , 443);
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_POST, 1);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));
$tuData = curl_exec($tuCurl);
curl_close($tuCurl);
$xml = simplexml_load_string($tuData);
print "<pre>";
print_r($xml);
?>
For more reference click on below link:
http://xmlpitest-ea.dhl.com/serviceval/jsps/main/Main_menu.jsp