curl-and-php
RE: Retrieving XML file through an HTTP url requiring athentication
Date: Fri, 2 Jan 2004 10:57:53 -0600
i found a more primitive fsockopen technique but then i found out it was a
problem with my isp restricting curl and fsockopen and i was getting a
permission denied error
here is the fsockopen technique that i was able to use at an isp that didn;t
support cURL
i also created a simple parser here without using xml_parser but it doesn;lt
do any error checking for input or output
but it worked fine for what i needed.
you will need to modify the custom xml start and the first part of post data
for your particular host
<?php
//script by Theron Patrick tpatrick_at_swbell.net
// pullpage function by Nick bouton http://www.nickbouton.com/.
$CustomerID = "123456";
$method = "POST";
$host = "[hostserver]"; //just ending in the .com not the whole url
$usepath="[path]"; //the relative path on the srve to the xml post file
example:/xml.asp
//add output tags as necessary to the xml_array.
//only tags in the xml_array will be output.
// They should be set to the matching input form variable so that the form
variable will be input or
//if no form variable submitted the computed output will be sent out
//sample inputs/output
$xml_array[Address]=$HTTP_POST_VARS[Address];
$xml_array[City]=$HTTP_POST_VARS[City];
$xml_array[State]=$HTTP_POST_VARS[State];
$xml_array[Zip]=$HTTP_POST_VARS[Zip];
// create first part of xml post data
$postdata .="<?xml version=\"1.0\" ?><RecordSet><CustomerID>". $CustomerID
."</CustomerID><Record>";
//create middle record data of xml post
while (list($key, $value) = each
(${"xml_array"}))
{
$postdata.= "<". $key.">" .$value."</".$key .">";
$output_array[$key]="";
}
// append last part of xml post data
$postdata .="</Record></RecordSet>";
// if custom xml disregard above and use custom xml instead
// also, you can comment out the form and post directly to this php script
// setting the posted xml variable name to custom_xml
if($custom_xml){
$postdata ="<?xml version=\"1.0\" ?><RecordSet><CustomerID>". $CustomerID
."</CustomerID>". $custom_xml . "</RecordSet>";
}
//if you do not want the form to show you can comment out the line below
//include('input_form.htm');
function pullpage( $method, $host, $usepath, $postdata) {
# open socket to filehandle
$fp = fsockopen( $host, 80, &$errno, &$errstr, 120 );
# user-agent name
//$ua = "Mozilla/1.0";
if( !$fp ) {
print "$errstr ($errno)<br>\n";
}
else {
if( $method == "GET" ) {
fputs( $fp, "GET $usepath HTTP/1.0\n");
}
else if( $method == "POST" ) {
fputs( $fp, "POST $usepath HTTP/1.0\n");
}
fputs( $fp, "User-Agent: ".$ua."\n");
fputs( $fp, "Accept: */*\n");
if( $method == "POST" ) {
$strlength = strlen($postdata);
fputs( $fp,
"Content-type: text/xml\n");
fputs( $fp, "Content-length: ".$strlength."\n\n");
fputs( $fp, $postdata."\n");
}
fputs( $fp, "\n");
$output = "";
# while content exists, keep retrieving document in 1K chunks
while( !feof( $fp ) ) {
$output .= fgets( $fp, 1024 );
}
fclose($fp);
}
return $output;
}
//To call pullpage() via HTTP POST, do the following:
$return_content = pullpage( "POST", $host,
$usepath , $postdata);
//echo $return_content . "<BR>end of xml output<BR>";
//create loop and extract single record for multiple record return
while (ereg('<Record>',$return_content)){
reset($output_array);
$start = "<Record>";
$end = "</Record>";
$startpos = strpos($return_content, $start);
$endpos = strpos($return_content, $end)+ strlen($end);
$record_content = trim(substr($return_content, $startpos,
$endpos-$startpos));
//remove record content from xml output
$return_content = trim(substr($return_content, $startpos
+strlen($record_content), strlen($return_content)));
//echo "Record content" . $record_content . "<BR>";
//echo "Return content" . $return_content . "<BR>";
echo "<B>NEW RECORD:</B><BR><table>";
while (list($key, $value) = each
(${"output_array"}))
{
$start ="<". $key.">";
$end = "</".$key .">";
$startpos = strpos($record_content, $start) + strlen($start);
$endpos = strpos($record_content, $end);
$output_array[$key] = trim(substr($record_content, $startpos,
$endpos-$startpos));
if (!strpos($record_content, $start)){$output_array[$key]="";}
// $output_array[$key] is the returned value for the $key you are looking
for
// for example $output_array['Price'] will give you the Price returned by
the xml server
// the example code echoed here puts the values into a table and creates a
separate table for each record
echo "
<TR><TD align = \"right\">".$key ."</TD><TD> -- </TD><TD>" .
$output_array[$key] ."</TD></TR>";
}
echo "</table>";
}
?>
-----Original Message-----
From: curl-and-php-admin_at_lists.sourceforge.net
[mailto:curl-and-php-admin_at_lists.sourceforge.net]On Behalf Of
Christian_Dudek/URIEL.MC_at_URIEL.MC
Sent: Friday, January 02, 2004 3:24 AM
To: curl-and-php_at_lists.sourceforge.net
Subject: Retrieving XML file through an HTTP url requiring athentication
Hello everybody and Happy New Year 2004!
I have a question for you all:
I need to retrieve an XML file from an ASP server, so that I can use
it with PHP and XSLT to use the informations the way I want.
The XML file is generated through an ASPX page under http and it
need a password to be accessible.
My problem is that if I use the address directly into my borwser
like:
http : // username:passw@ www .thesite.com /ghj/trioze.aspx , my
browser displays correctly the page.
Now, using cURL with this code:
echo "Initializing cURL session...<br>";
$ch = curl_init("http : // www .thesite.com /ghj/trioze.aspx ");
echo "Setting cURL options...<br>";
//curl_setopt($ch, CURLOPT_URL, "http : // www .thesite.com
/ghj/trioze.aspx ");
curl_setopt($ch, CURLOPT_USERPWD, "username:passw");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 1);
echo "Executing cURL options...<br>";
curl_exec($ch);
curl_close($ch);
fclose($fp);
I get a 401 Acces Denied error. I know the aspx request is made
through the 'get' method. I know also that cURL is using the same method.
Why does it not work? Is there any additionnal informations I need to send
with the curl_setopt()? Maybe it is too obvious and I am not getting it.
If anybody can help, that would be fantastic!
Many thanks anyway!
Christian
-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
Free Linux Tutorials. Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
Received on 2004-01-02