Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Post an XML file to a URL via PHP

Status
Not open for further replies.
can you clarify? you have asked in the subject for help posting a file, and in the body for help parsing an xml struct.
 
Hi.
I have an XML form that I need to send to a URL so they can read it and then return the results back to my webpage in the form of another XML file.
 
sounds like you need to use cURL.
you haven't given us enough information to be able to do more to help. we would need the api definition of your receiving server and the shape of your current xml input. personally, i avoid servers that require xml. i cannot see the advantage over name/value pairs or SOAP.
 
Hello,

Thank for the cURL tip, I'll look into that, but I need to look at getting this done using normal PHP for the moment.
I've been trying to use the HttpRequest::METH_POST command, but I get a error back saying :-

Parse error: syntax error, unexpected ')', expecting '(' in d2xml.php on line 192

d2xml.pnp look like this

<?php
if(isset($_POST['create_xml'])){

echo "Links Data Posted";

/* All Links data from the form is now being stored in variables in string format */

$firstName = $_POST['firstName'];

$lastName = $_POST['lastName'];

$address1 = $_POST['address1'];

$address2 = $_POST['address2'];

$phone = $_POST['phone'];

$email = $_POST['email'];

$CrqDateTime=str_replace('%',substr(microtime(),2,7),date('Y-m-d\TH:i:s.%O'));

$CrqDateTime=substr($CrqDateTime,0,-2).':'.substr($CrqDateTime,-2);

$CrqDocName=str_replace('%',substr(microtime(),2,7),date('Y-m-d\TH-i-s.%O'));

$CrqDocName=substr($CrqDocName,0,-2).':'.substr($CrqDocName,-2);

$urlDoc = $CrqDocName;

$url = "
$CrqTestRequest = "Test";

$CrqService = "Identity";

$CrqCompanyID = "TestCompany";

$CrqUserID = "TestUser";

$CrqSearchID = "username";

$CrqPassword = "TestPwd";

$CrqClientRef = "password";

$xmlBeg1 = '<?xml version="1.0" encoding="utf-8"?>';

$xmlBeg2 = '<soap:Envelope xmlns:xds="
xmlns:xsi=" xmlns:soap="
$xmlBeg4 = '<PerformIDCheck xmlns=" url">';

$CrqIntegratorID ="another password";

$xml_document= $xmlBeg1;

$xml_document .= $xmlBeg2;

$xml_document .= "<soap:Body>";

$xml_document .= $xmlBeg4;

$xml_document .= "<CallRequest>";

$xml_document .= "<CrqIntegratorID>";

$xml_document .= $CrqIntegratorID;

$xml_document .= "</CrqIntegratorID>";

$xml_document .= "<CrqTestRequest>";

$xml_document .= $CrqTestRequest;

$xml_document .= "</CrqTestRequest>";

$xml_document .= "<CrqDateTime>";

$xml_document .= $CrqDateTime;

$xml_document .= "</CrqDateTime>";

$xml_document .= "<CrqService>";

$xml_document .= $CrqService;

$xml_document .= "</CrqService>";

$xml_document .= "<CrqCompanyID>";

$xml_document .= $CrqCompanyID;

$xml_document .= "</CrqCompanyID>";

$xml_document .= "<CrqUserID>";

$xml_document .= $CrqUserID;

$xml_document .= "</CrqUserID>";

$xml_document .= "<CrqPassword>";

$xml_document .= $CrqPassword;

$xml_document .= "</CrqPassword>";

$xml_document .= "<CrqSearchID>";

$xml_document .= $CrqSearchID;

$xml_document .= "</CrqSearchID>";

$xml_document .= "<CrqClientRef>";

$xml_document .= $CrqClientRef;

$xml_document .= "</CrqClientRef>";

$xml_document .= "<CrqApplicant>";

$xml_document .= "<AptName>";

$xml_document .= "<Surname>";

$xml_document .= $lastName;

$xml_document .= "</Surname>";

$xml_document .= "<Forename>";

$xml_document .= $firstName;

$xml_document .= "</Forename>";

$xml_document .= "<Title>";

$xml_document .= $email;

$xml_document .= "</Title>";

$xml_document .= "</AptName>";

$xml_document .= "<AptAddress>";

$xml_document .= "<AadBuilding>";

$xml_document .= $phone;

$xml_document .= "</AadBuilding>";

$xml_document .= "<AadStreet>";

$xml_document .= $address1;

$xml_document .= "</AadStreet>";

$xml_document .= "<AadPostTown>";

$xml_document .= $address2;

$xml_document .= "</AadPostTown>";

$xml_document .= "<AadPostCode>";

$xml_document .= $phone;

$xml_document .= "</AadPostCode>";

$xml_document .= "</AptAddress>";

$xml_document .= "</CrqApplicant>";

$xml_document .= "</CallRequest>";

$xml_document .= "</PerformIDCheck>";

$xml_document .= "</soap:Body>";

$xml_document .= "</soap:Envelope>";

$path_dir = "output/";

$path_dir .= $urlDoc .".xml";

/* Data in Variables ready to be written to an XML file */

$fp = fopen($path_dir,'w');

$write = fwrite($fp,$xml_document);
echo ". Posted filename is " .$path_dir;

/* Send File */
$req = &new HttpRequest($url, HttpRequest::METH_POST);
$req->addHeaders(("POST /webpage.asmx HTTP/1.1"));
$req->addHeaders(("Host: ws.webpage2.co.uk"));
$req->addHeaders(("Content-Type:text/xml; charset=utf-8"));
$req->addHeaders(("Content-Length: 999"));
$req->addHeaders(('SOAPAction:"$req->addPostFile(($url.".xml"));
$req->send();
echo $req->getResponseBody();
/* Send File */

}

?>


line 197 is above the /* send file */ line, so its either the line above that, or below, but both look ok to me.
I KNOW i've typed something in wrong, but for the life of me I cannot see what.
Can someone point out my error to me?

Thanks
 
Code:
$req = new HttpRequest($url, HTTP_METH_POST); //this line changed
$req->addHeaders(("POST /webpage.asmx HTTP/1.1"));
$req->addHeaders(("Host: ws.webpage2.co.uk"));
$req->addHeaders(("Content-Type:text/xml; charset=utf-8"));
$req->addHeaders(("Content-Length: 999"));
$req->addHeaders(('SOAPAction:"[URL unfurl="true"]http://webpage"'));[/URL]
$req->addPostFile(($url.".xml"));
$req->send();
echo $req->getResponseBody();
 
Hi jpadie,

Thanks for point out my error. A freash pair on eyes on a problem always seems to help.

I made the change, but i get a new error now. It says :-

Fatal error: Cannot instantiate non-existent class: httprequest in d2xml.php on line 192.

Ant ideas on that one ?

Thanks again
 
i did think that might be a problem.

http extension is not bundled with php. you need to install it via pecl. if you do not have root access you will not be able to do this. if you do have root access then it should be easy. let us know.

i still think that cURL is a much better bet. much easier too!
 
Hi,
how do I install in via pecl?( wil have to bear with me. I'm a COMPLETE noob with it comes to PHP ).
Thanks
 
Out of interest, how would I turn the above coding into cURL so that it did the same job ?

Thanks
 
just realised that this is a SOAP question really. you could still do in cURL, but have you taken a look at
for installation of httprequest, you'd do something like
Code:
pecl install --alldeps httprequest

you may need to sudo this, depending on your installation. you also, obviously, need root access.

for cURL examples see
 
it turns out i cant get root access to the server ( it's a 3rd party host, and other pages are looked after by the same unit ).
I've been trying to load PEAR up via ftp to get httprequest installed, but am having trouble getting the php page to install.
Any ideas ?
 
pear will not install pecl stuff. you need root access for pecl. period.

you will need to use the soap extension or the curl extension.
 
seeing that you are championing cURL, how would i "cURL"ize the code I have so far to do the same job:-

>>>>
$req = new HttpRequest($url, HTTP_METH_POST); //this line changed
$req->addHeaders(("POST /webpage.asmx HTTP/1.1"));
$req->addHeaders(("Host: ws.webpage2.co.uk"));
$req->addHeaders(("Content-Type:text/xml; charset=utf-8"));
$req->addHeaders(("Content-Length: 999"));
$req->addHeaders(('SOAPAction:"$req->addPostFile(($url.".xml"));
$req->send();
echo $req->getResponseBody();
 
did you not read the curl part of the manual that I posted?
 
hello,

I must admit i skimmed the manual rather than reading it. My fault. sorry. Learning one programming language on the fly is one thing, but 2 is quite a different kettle of fish and might just fry my brain in the state that it's in at the moment.

Thought help from my ISP i've got PEAR installed and http_request now works for the bove code ( thank you for your help point this out ).
The addheaders needed to be recoded to addHeader , but I'm now getting :-

Warning: Missing argument 2 for HTTP_Request::addHeader()

I believe this is down to me not coding the addheader command correctly ( ie addHeader("X-PHP-Version", phpversion());)
but I dont know how to code the above addheaders command so that they output as they apeer in the qoutes.

Can you possabley help me with that ?
 
::UPDATE::

I've worked out the addHeader problem, I think ( as I'm not getting an error message now ), but I am now being told that :-

Fatal error: Call to a member function getMessage() on a non-object in test.php on line 203

( Line 203 reads :- echo $response->getMessage();).

line 202 reads echo $req->getResponseBody();, but i'm not getting any output from either .
 
I cannot see any getMessage() method within the class definition for httprequest. this will be why you are getting the error.

the correct method to capture the response body seems to be getResponseBody(). if you are not getting any errors and you are not getting any content then the chances are that there is no response from the server.

separately I believe that you are misusing AddHeaders(). it should be an array of key->value pairs that are passed
Code:
$req->addHeaders(array("X-PHP-Version"=>phpversion())));

addPostFile takes a minimum of two arguments. name and path.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top