Just another WordPress.com site

Recently I was working for a product where i have to post a file to a remote server where remote server dont allow outbout FTP.
And in this process i found a couple of methods to post file to server;

my required file was in Xml and have to post that xml to remote server where I can fetch that data
Its quite simple and easy method.

here are my findings

local file name : postfile.php
remote file name : receivexml.php

code for postfile.php

$thistext=”<?xml version=”1.0″?>
                        <expiry_message>Expiry Message</expiry_message>

$ch = curl_init(‘http://rem.ote.server.ip/receivexml.php&#8217;);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $thistext);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/xml’,
‘Content-Length: ‘ . strlen($thistext)) );                                                                                                                  
$result = curl_exec($ch);

code for  receivexml.php

echo “test xml”;
    $postText = file_get_contents(‘php://input’);
$xml = new SimpleXMLElement($postText);
$result = $xml->xpath(‘/SERVICES/ serv1234/short’);
while(list( , $node) = each($result)) {
    echo $short=$node;
$myFile = “/directory/path/to/xml/”.$short.”.xml”;
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);
fwrite($fh, $stringData);

in receivexml.php

  1. received the file and stored in  $postText
  2. parsed the xml data with xpath
  3. set the file name to a tag value in this case “short”
  4. write data to file.


Comments on: "post a file using PHP to remote server without ftp" (3)

  1. Hey, thanks so much.. this helped me alot…

  2. Why are you spamming links to this blog? Please stop.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: