How to do a POST with XML API? - php

Here we see: https://developers.google.com/youtube/2.0/developers_guide_protocol_comments#Adding_a_comment
I need to do a request with XML API.
POST /feeds/api/videos/VIDEO_ID/comments HTTP/1.1
Host: gdata.youtube.com
Content-Type: application/atom+xml
Content-Length: CONTENT_LENGTH
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<content>This is a crazy video.</content>
</entry>
What should I use for this?

You can use cURL to do this.
<?php
$data = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<content>This is a crazy video.</content>
</entry>
XML;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/atom+xml',
'Authorization: Bearer ACCESS_TOKEN',
'GData-Version: 2',
'X-GData-Key: key=DEVELOPER_KEY'));
$re = curl_exec($ch);
curl_close($ch);
?>

You'll likely find it easiest to use one of the client libraries rather than do the POST manually, because it will handle header generation, authentication, and tokens for you without a lot of hassle. A list of the client libraries is here:
https://developers.google.com/youtube/code
For example, to do a comment post with the Python client, it would look something like this (assuming you'd gone through the authentication steps, which the clients make pretty simple):
my_comment = 'what a boring test video'
video_id = '9g6buYJTt_g'
video_entry = yt_service.GetYouTubeVideoEntry(video_id=video_id)
yt_service.AddComment(comment_text=my_comment, video_entry=video_entry)
The clients for other languages follow the same structure.

Related

Can't Extract Data from SOAP Response

I have been working on a SOAP request / response using CURL with PHP, however I can't seem to work out how to extract the Data for the life of me.
Script for SOAP Request
$soapUrl = "https://xml.proveid.experian.com/IDSearch.cfc?wdsl";
// xml post structure
$xml_post_string = "<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:cor='http://corpwsdl.oneninetwo'>
<soapenv:Header/>
<soapenv:Body>
<cor:search soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
<xml xsi:type='xsd:string'><![CDATA[
<Search xmlns:xsd='https://xml.proveid.experian.com/xsd/Search.xsd'>
<Authentication>
<Username>".$user."</Username>
<Password>".$pw."</Password>
</Authentication>
<CountryCode>GBR</CountryCode>
<Person>
<Name>
<Forename>".$firstName."</Forename>
<Surname>".$secondName."</Surname>
</Name>
<DateOfBirth>".$dob."</DateOfBirth>
</Person>
<Addresses>
<Address Current='1'>
<Premise>".$premise."</Premise>
<Street>".$streetName."</Street>
<PostTown>".$postTown."</PostTown>
<Region/>
<Postcode>".$postCode."</Postcode>
<CountryCode>GBR</CountryCode>
</Address>
</Addresses>
<Telephones>
<Telephone>
<Number>".$telephone."</Number>
</Telephone>
</Telephones>
<IPAddress>127.0.0.1</IPAddress>
<Emails>
<Email>".$email."</Email>
</Emails>
<SearchOptions>
<ProductCode>".$product."</ProductCode>
</SearchOptions>
</Search>]]>
</xml>
</cor:search>
</soapenv:Body>
</soapenv:Envelope>";
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: https://xml.proveid.experian.com/IDSearch.cfc",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
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);
curl_close($ch);
print_r($response);`
Response
<Search xmlns:xsd="https://xml.proveid.experian.com/xsd/Search.xsd">
<CountryCode>GBR</CountryCode>
<Person>
<Name><Forename>Baken</Forename>
<Surname>Jorure</Surname>
</Name>
<DateOfBirth>1989-09-09</DateOfBirth>
<Age>26</Age>
</Person>
<Addresses>
<Address Current="1">
<Premise>410</Premise>
<Street>Beuno Terrace</Street>
<PostTown>Beuno</PostTown>
<Region/>
<Postcode>LL545BT</Postcode>
<CountryCode>GBR</CountryCode>
</Address>
</Addresses>
<Telephones>
<Telephone>
<Number>01154567892</Number>
</Telephone>
</Telephones>
<IPAddress>127.0.0.1</IPAddress>
<Emails>
<Email>bakenjorure#www.com</Email>
</Emails>
<SearchOptions>
<ProductCode>ProveID</ProductCode>
</SearchOptions>
<OurReference>B3C369C0-F001-4FB1-80D3-801CB9D872FE</OurReference>
<SearchDate>2015-10-09T23:46:54</SearchDate>
<Result>
<Summary>
<ReportSummary>
<DatablocksSummary>
<DatablockSummary>
<Name>CreditReference</Name>
<Decision/>
</DatablockSummary>
</DatablocksSummary>
</ReportSummary>
<DecisionMatrix Code="ECIGSUAT" Name="Electronic Cigarettes">
<Decision>
<Outcome Type="Primary">0</Outcome>
<Reason>Individuals DOB has not matched to active CAIS
ER or Citizen Card therefore application has been
referred
</Reason>
</Decision>
</DecisionMatrix>
</Summary>
</Result>
</Search>
Now all I really need it the number in the Outcome tags. I've tried so many ways but I just can't seem to get it.
Any help would be much appreciated.
Thanks in advance.
For simplier usage of SOAP, you can use PHP builtin SOAP client.
http://php.net/manual/en/class.soapclient.php
With SoapClient class you could do something like this:
<?php
$soapUrl = "https://xml.proveid.experian.com/IDSearch.cfc?wdsl"
$soapClient = new SoapClient($soapUrl);
$parameters = array();
$result = $soapClient->Search($parameters);
?>
In result you will get native PHP data structure, not plain text XML as in here.

PHP & XML - How to generate a soap request in PHP from this XML?

I am completly new to SOAP operations.
I have been provided with an XML document (SOAP) to get some collection points for a shipping method.
From the manual located here:
http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint
I can see that I need to use the following SOAP request:
POST /package/package_1.3/packageservices.asmx HTTP/1.1
Host: privpakservices.schenker.nu
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://privpakservices.schenker.nu/SearchCollectionPoint"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/">
<customerID>long</customerID>
<key>string</key>
<serviceID>string</serviceID>
<paramID>int</paramID>
<address>string</address>
<postcode>string</postcode>
<city>string</city>
<maxhits>int</maxhits>
</SearchCollectionPoint>
</soap:Body>
</soap:Envelope>
The thing is that i don't know how to send this as a request using PHP, and how to get the response.
Any help to pinpoint me in the right direction, is much appreciated.
UPDATE
I can read the response data with var_dump. However, I am not able to read individual element data.
I need to read data as below
foreach($parser as $row) {
echo $row->customerID;
echo $row->key;
echo $row->serviceID;
}
If anyone should be interested, i have provided the correct answer:
$soapUrl = "http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint";
$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><SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/"><customerID>XXX</customerID><key>XXXXXX-XXXXXX</key><serviceID></serviceID><paramID>0</paramID><address>RiksvŠgen 5</address><postcode>59018</postcode><city>Mantorp</city><maxhits>10</maxhits></SearchCollectionPoint></soap12:Body></soap12:Envelope>';
$headers = array(
"POST /package/package_1.3/packageservices.asmx HTTP/1.1",
"Host: privpakservices.schenker.nu",
"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);
following example might help you.
SOAP XML schema
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.dataaccess.com/webservicesserver/">
<x:Header/>
<x:Body>
<web:NumberToDollars>
<web:dNum>10</web:dNum>
</web:NumberToDollars>
</x:Body>
</x:Envelope>
PHP code
$wsdl = 'http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL';
try{
$clinet=new SoapClient($wsdl);
$ver =array("dNum"=>"2002");
$quates=$clinet->NumberToDollars($ver);
var_dump($quates);
}
catch(SoapFault $e)
{
echo $e->getMessage();
}

Sending XML via php and getting back a response

I'm having problems sending the follwoing XML via PHP. I can't seem to get the script right it just comes back with a blank page. Any suggestions would be great. I am not coming up with any errors just nothing seems to happen.
POST /PubServices/WebServices/PublicationWebServices.asmx HTTP/1.1
Host: www.geminifund.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
POST /PubServices/WebServices/PublicationWebServices.asmx HTTP/1.1
Host: www.geminifund.com
Content-Type: application/soap+xml; charset= utf-8
Content-Length: length
<?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>
<GetHistroricalNav xmlns="http://www.geminifund.com/webservices/">
<startDate>dateTime</startDate>
<endDate>dateTime</endDate>
<portfolio>string</portfolio>
</GetHistroricalNav>
</soap12:Body>
</soap12:Envelope>
and my script is as follows
<?php
$url = "http://www.geminifund.com";
$post_string = '<?xml version="1.0" encoding="UTF-8"?>
<rootNode>
<innerNode>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetHistroricalNav xmlns="http://www.geminifund.com/webservices/">
<startDate>2011-06-30</startDate>
<endDate>2011-7-30</endDate>
<portfolio>1778/portfolio>
</GetHistroricalNav>
</soap:Body>
</soap:Envelope>
</innerNode>
</rootNode>';
$header = "POST /PubServices/WebServices/PublicationWebServices.asmx HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $post_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
?>
When you write "it just comes back with a blank page " are you referring to a web browser response when you enter the url? If this is what you are referring to, what web browser are you using? I recommend you try different web browsers (Safari, FireFox, Chrome) and see what results you get.
Why are you using headers to set a POST method and doing a custom request? Curl is perfectly capable of doing a POST as is:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
custom request is for when you want to do a less-common method, such as a HEAD or PUT request.
and PHP have SoapClient class to handle SOAP protocol. Are you sure you want to do it in hand-written manner, friend? :)

Getting a POST requests require a Content-length header response(411 error)

I have been trying to upload video on video on you tube using my own code.
During one step when i try to upload videos meta data on you tube to get the token and url for the video to upload i get a
POST requests require a Content-length header. That’s all we know.
(411)
My code is as follows:
$xml = '<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">Video.loc</media:title>
<media:description type="plain">
Video.loc first vid
</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>Video.loc</media:keywords>
</media:group>
</entry>';
$headers = array('Authorization: AuthSub token="'.$_SESSION['AuthSubSessToken'],
'GData-Version: 2',
'X-GData-Key: key='.$key,
'Content-length:'.strlen($xml),
'Content-Type:application/atom+xml; charset=UTF-8'
);
$curl = curl_init('http://gdata.youtube.com/action/GetUploadToken');
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_HEADER, 0);
$response = simplexml_load_string(curl_exec($curl));
$response = curl_exec($curl);
$arrToken = explode('=',$response);
print('<pre>');print_r($arrToken);die();
curl_close($curl)
Does any one knows that if it is error on my side or on the google?.
Regards
Himanshu Sharma
You are missing a closing " after
AuthSub token="'.$_SESSION['AuthSubSessToken'],
that might be messing up the headers.

Implement SOAP 1.1 in php

The following is a sample SOAP 1.1 request and response
POST /DEMOWebServices2.8/service.asmx HTTP/1.1
Host: api.efxnow.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://api.efxnow.com/webservices2.3/DealRequestAtBest"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<Authenticator xmlns="https://api.efxnow.com/webservices2.3">
<ApplicationName>string</ApplicationName>
<IPAddress>string</IPAddress>
<UserID>string</UserID>
<MachineName>string</MachineName>
</Authenticator>
</soap:Header>
<soap:Body>
<DealRequestAtBest xmlns="https://api.efxnow.com/webservices2.3">
<UserID>string</UserID>
<PWD>string</PWD>
<Pair>string</Pair>
</DealRequestAtBest>
</soap:Body>
</soap:Envelope>
Response -
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<DealRequestAtBestResponse xmlns="https://api.efxnow.com/webservices2.3">
<DealRequestAtBestResult>
<Success>boolean</Success>
<ErrorDescription>string</ErrorDescription>
<ErrorNumber>int</ErrorNumber>
<Confirmation>string</Confirmation>
<ConfirmationNumber>string</ConfirmationNumber>
<Rate>double</Rate>
</DealRequestAtBestResult>
</DealRequestAtBestResponse>
</soap:Body>
</soap:Envelope>
i want to know how to make the request and how to handle response if this had to be done in php. i read this but i can't figure out how would __setSoapHeaders() and __call() be implemented in my case. thanks in advance.
There's a SOAP library for PHP, but for a simple exchange you might consider building XML request body as a string and the dispatch it using the curl library function. It's a much more low-level network api, which I at least find easier to use. Note that PHP needs to be compiled --with-curl[=DIR].
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ((bool)$proxy) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Pragma:','Cache-Control:'));
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
// Apply the XML to our curl call
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$out = curl_exec($ch);
curl_close($ch);
?>

Categories