I use RESTful webservice and it's show some xml.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <users>
- <user>
<id>1</id>
<name>Mahesh</name>
<profession>Teacher</profession>
</user>
</users>
I want to add new xml, send to the webservice.
$xml =
'<users>
<user>
<id>3</id>
<name>hangga</name>
<profession>IT</profession>
</user>
</users>';
Here my php code :
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost:18080/UserManagement/rest/UserService/users");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); //setting content type header
curl_setopt($curl, CURLOPT_POST, $xml);//Setting raw post data as xml
$result = curl_exec($curl);
curl_close($curl);
print($result);
But nothing happens, web service not show new XML. My question how to add new xml and send it to web service using php? should I use cUrl? is there another way? I really need help, the answer will very appreciated. Thanks all
$url = 'http://webservice/example/user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml',
'Content-Length: ' . strlen($data))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$responses = curl_exec($ch);
curl_close($ch);
Related
I am trying to post XML request to ingram micro url the code shown below
<?php
$url = "https://newport.ingrammicro.com/mustang";
$post_string = '<BaseRateRequest>
<Version1.0></Version1.0>
<TransactionHeader>
<CountryCode>FT</CountryCode>
<LoginID>username</LoginID>
<Password>password</Password>
<TransactionID>TESTAIC12356</TransactionID>
</TransactionHeader>
<BaseRateInformation>
<BranchOrderNumber>4066000</BranchOrderNumber>
<PostalCode>L5R1V4</PostalCode>
<Suffix />
</BaseRateInformation>
</BaseRateRequest>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=".$post_string);
$data = curl_exec($ch);
print_r($data);
?>
But, I don't know how to request and response in XML. I am getting the error: Invalid Inbound XML Document.
What does that means and how to solve it ?
you have to remove "XML="
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
and probably also add the XML header:
<?xml version="1.0" encoding="UTF-8"?>
<BaseRateRequest>
...
</BaseRateRequest>
I am trying to call a web service through php . I am sending an XML request for which I am supposed to get back an Xml response. But I as response I am getting only the data without the xml tags. I want it in proper xml format. I also have the wsdl file of the website. If anyone can advise what to do it will be of real help. Thanks in advance for your time.
<?php
$soapUrl=some url';
$xml_post_string='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sec="http://schemas.xmlsoap.org/ws/2002/04/secext" xmlns:urn="urn:ebx-schemas:dataservices_1.0">
<soapenv:Header>
<sec:Security>
<UsernameToken>
<Username>Some Value</Username>
<Password>Some Value</Password>
</UsernameToken>
</sec:Security>
</soapenv:Header>
<soapenv:Body>
<urn:select_ProductDetail>
<branch>Product</branch>
<instance>Product</instance>
<viewPublication>List Name</viewPublication>
</urn:select_ProductDetail>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-Type: text/xml;charset=UTF-8",
"Accept: gzip,deflate",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"select\"",
"Host: Host name",
"Connection: Keep-Alive",
"Content-length: ".strlen($xml_post_string),
);
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($ch, CURLOPT_MAXREDIRS, 12);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ss = curl_getinfo($ch);
$response = curl_exec($ch);
print_r($response);
exit;
curl_close($ch);
?>
Try modifying your codes to below, for viewing it in BROWSERS
$ss = curl_getinfo($ch);
$response = curl_exec($ch);
echo '<pre>';
echo htmlspecialchars(print_r($response, true));
echo '</pre>';
#print_r($response);
exit;
curl_close($ch);
You are not getting output in xml format because output is getting concatenated with header of curl to avoid so use below code
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($ch, CURLOPT_MAXREDIRS, 12);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ss = curl_getinfo($ch);
//$response will give you xml format code
$response = curl_exec($ch);
//$xml will create array from that xml format code
$xml=simplexml_load_string($response);
print_r($xml);
exit;
curl_close($ch);
I want to subscribe a YouTube Channel through the GData API via PHP.
How can i do this post in PHP?
I tried it like this but the page keeps loading forever:
<?php
session_start();
include 'gdata.php' // For $DEVKEY
function post_xml($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array(
"Content-Type: application/atom+xml",
"Content-Length: 1024",
"Authorization: Bearer $token",
"GData-Version: 2",
"X-GData-Key: key=$DEVKEY"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $xml );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $result;
}
$token = $_SESSION['token'];
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat"
term="channel"/>
<yt:username>GoogleDevelopers</yt:username>
</entry>';
$url = 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
echo post_xml($url, $xml);
?>
In HttpRequester i already managed it to do a HTTP Post Request and it worked. I think the problem is the content of the Request. How do i properly give the text which is in "Content" (look at the screenshot) via PHP (cURL)?
Thanks :)
Just put the $xml as POSTFIELDS and it should work:
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
And add the correct content-length:
"Content-Length: ".strlen($xml),
When you're planning to do more, like PUT-requests with data and just lots of REST-requests, I would suggest to use some kind of package like Httpful. Curl has it's pitfalls...
You can look at the code below, hope it will help
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res);
Here my code is,
$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_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close ($ch);
print_r($response);
I got response like this
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
<mediaid>af11zj</mediaid>
<mediaurl>http://yfrog.com/af11zj</mediaurl>
</rsp>
How to get that mediaurl ? Any one help me? Thanks.
Assume you have the response XML in $response:
<?php
$obj = simplexml_load_string($response);
$mediaurl = (string) $obj->mediaurl;
?>
I need to receive xml data from server.
Allowed only two methods to send data to server "GET" AND "PUT"
POST method- is no allowed.
My code is
<?php
set_time_limit(0);
ini_set('display_errors','1');
error_reporting(E_ALL);
$url = 'https://url is here..';
$fp = fopen(getcwd() . "/ping.xml", "r+");
// Initialize session and set URL.
$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, $url.'?xml='.$xml);
curl_setopt($ch, CURLOPT_URL, $url);
//GET
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
//curl_setopt($ch, CURLOPT_INFILE, $fp);
//curl_setopt($ch, CURLOPT_INFILESIZE, filesize(getcwd() . "/ping.xml"));
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_SSLVERSION, 3 );
curl_setopt($ch, CURLOPT_VERBOSE, '1');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . "/certs/test_lv.pem");
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "test_lv");
curl_setopt($ch, CURLOPT_SSLKEY, getcwd() . "/certs/test_lv.key");
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, "test_lv");
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml','correlationId :'.date("y-m-d-h-m-s")));
curl_setopt($ch, CURLOPT_HEADER, 1);
// Get the response and close the channel.
$response = curl_exec($ch);
echo curl_error($ch);
print_r($response);
echo '<br>end';
curl_close($ch);
?>
I have spent all my day connecting to the server and now it works.
I trying to send xml file as Put method
Xml file data:
<?xml version="1.0" encoding="UTF-8"?>
<Ping>
<Value>Test</Value>
</Ping>
Its not work I have this error:
HTTP/1.1 100 Continue HTTP/1.1 500 Internal Server Error Content-Length: 0 Server: Jetty(6.1.21)
end
Answers needs to be:
Received pong.xml-s
<?xml version="1.0" encoding="UTF-8"?>
<B4B xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://test.net/valid/hgw-response.xsd">
<Pong from="EE">
<Value>Test</Value>
</Pong></B4B>
Thanks for advices
P.S How can i Send xml file via GET method ..Im trying to do this
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Ping>
<Value>Test</Value>
</Ping>';
curl_setopt($ch, CURLOPT_URL, $url.'?xml='.$xml);
It's not work too for me.