-
August 12th, 2005, 11:51 AM
#1
Posting to URL using PHP
Hi,
I wish to send some XML (preformatted into a string) to a URL using PHP.
I have the following code to illustrate my requirement, but having spent last hour googling I can't find anything suitable in php. Any ideas? Is it possible? Thanks :-)
Code:
private static string DoPost(string url,string data)
{
bool got_exception=false;
data="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + data;
String result = "";
StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
Rpc.Rpc.SetWebProxy(objRequest);
objRequest.Timeout=60000;
objRequest.Method = "POST";
objRequest.Credentials = CredentialCache.DefaultCredentials;
objRequest.ContentType = "text/xml";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(data);
}
catch(System.Net.WebException we)
{ got_exception=true;
if(we.Status!=WebExceptionStatus.Timeout)
throw we;
}
catch (Exception e)
{
throw e;
}
finally
{
try
{
myWriter.Close();
}
catch(Exception)
{}
}
if(got_exception) return "";
try
{
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
Last edited by Ravanol; August 12th, 2005 at 12:00 PM.
-
August 12th, 2005, 01:11 PM
#2
Re: Posting to URL using PHP
Here you go:
Code:
//
// Illustrates the steps you take to POST with PHP...
//
function DoPost($data)
{
// fix up your data like you like it
$data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".$data;
// now make a POST request
$send = "POST /whereever.php HTTP/1.1\r\n";
$send .= "Host: http://www.somesite.com \r\n";
$send .= "Content-type: application/x-www-form- urlencoded\r\n";
$send .= "Content-length: " . strlen($data) . "\r\n";
$send .= "Connection: close\r\n\r\n";
$send .= $data;
// now connect to the place where you want to send it
$fp = fsockopen("http://www.somesite.com", 80);
// now send it
fputs($fp, $send);
//now read the result
$result = "";
while (!feof($fp)) {
$result .= fgets($fp,128);
}
fclose($fp);
//now return the result
return $result;
}
This is something pretty close to what you want, I did not try and debug it, but I've done code like that before. Use that as an example and you can do it, but keep in mind I did not actually try that code sample on my site to debug it or not, its just to show you the steps you have to do.
-
August 12th, 2005, 01:44 PM
#3
Re: Posting to URL using PHP
Brilliant.. thank you so much!!
The trouble with this web site.. especially after you have spent 2 hours trying to work something out... is that it is too tempting to come here and ask your question and save the effort!!! ;-)
Actually, no.. I won't do that because I won't learn properly... but it is tempting.
Thanks again, I am off to decipher it all
A.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|