Welcome, Guest. Please login or register.

Author Topic: Posting XML data to aspx script using PHP  (Read 3828 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Posting XML data to aspx script using PHP
« on: November 04, 2009, 09:24:07 PM »
I've been trying to figure this out for ages, but just can't get my head around it. I'm trying to write a script which will generate a PDF copy of the Metro newspaper. As far as I can tell, their Flash application posts the data contained in the following file:

http://e-edition.metro.co.uk/YYYY/MM/DD/edition.xml

(with YYYY, MM and DD set to the desired date), to the following script:

http://edition.pagesuite-professional.co.uk/create_pdf.aspx?

which then generates the PDF and returns HTML code providing a link to the file. The file is generated on the fly with a name which does not correspond to anything guessable, so the script has to be run. In other words, there is no existing PDF you can hotlink to.

I can't for the life of me figure out how this is done. I've used Firebug to capture the POST request, and the details are below. There is quite a bit about this on the Internet, but most of the examples I found rely on PHP extensions which only seem to work on Windows, or are non-working pseudo-code.


Headers:
Response Headers
Server   Microsoft-IIS/6.0
X-Powered-By   ASP.NET
X-AspNet-Version   2.0.50727
Cache-Control   private
Content-Type   text/html; charset=utf-8
Content-Length   226
Date   Wed, 04 Nov 2009 20:10:06 GMT
Connection   keep-alive
Vary   Accept-Encoding

Request Headers
Host   edition.pagesuite-professional.co.uk
User-Agent   Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-GB; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Accept   text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language   en-gb,en;q=0.5
Accept-Encoding   gzip,deflate
Accept-Charset   ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive   300
Connection   keep-alive
Referer   http://e-edition.metro.co.uk/2009/11/04/


Data:
Content-type: application/xml
Content-length: 2415

Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Posting XML data to aspx script using PHP
« Reply #1 on: November 04, 2009, 11:02:21 PM »
Metro fans, rejoice! The following works.

<?php

$year
=date('Y');
$month=date('m');
$day=date('d');

$xml file_get_contents("http://e-edition.metro.co.uk/$year/$month/$day/edition.xml");

$ch curl_init(); //initiate the curl session
curl_setopt($chCURLOPT_URL"http://edition.pagesuite-professional.co.uk/create_pdf.aspx?"); //set to url to post to
curl_setopt($chCURLOPT_RETURNTRANSFER1); // tell curl to return data in a variable
curl_setopt($chCURLOPT_HEADER0);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS$xml); // post the xml
curl_setopt($chCURLOPT_TIMEOUT5); // set timeout in seconds
$xmlResponse curl_exec ($ch);
curl_close ($ch);

$start=strpos($xmlResponse"http");
$url=substr($xmlResponse$start);
$end=strpos($url".pdf");
$url=substr($url0$end+4);

echo 
"<a href='" $url "'>Click here to download the Metro</a>";

?>
« Last Edit: November 04, 2009, 11:09:13 PM by motorollin »
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline mandm

  • Newbie
  • *
  • Join Date: Jul 2010
  • Posts: 2
    • Show only replies by mandm
Re: Posting XML data to aspx script using PHP
« Reply #2 on: July 10, 2010, 04:34:42 AM »
This code somehow does not work for me?...did it work for anyone?
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Posting XML data to aspx script using PHP
« Reply #3 on: July 10, 2010, 04:58:30 PM »
Quote from: mandm;569751
This code somehow does not work for me?...did it work for anyone?


You'll at least need fopen URL wrappers enabled in php or it will fail at the first stage.
int p; // A
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Posting XML data to aspx script using PHP
« Reply #4 on: July 11, 2010, 02:56:29 PM »
Just tested and it still works for me. As Karlos said make sure you have correctly configured the PHP service on your web server, and also only use it on a date when there was actually a Metro published. The script uses the current date as part of the URL string for the current issue, and will get a 404 if you run it on a Saturday or Sunday. If you want to test the script on a weekend you can force it to download a particular issue by manipulating the $xml variable like this:

Code: [Select]
$xml = file_get_contents(&quot;http://e-edition.metro.co.uk/2010/07/09/edition.xml&quot;);

which would download the issue from Friday just gone (the 9th of July 2010).

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline mandm

  • Newbie
  • *
  • Join Date: Jul 2010
  • Posts: 2
    • Show only replies by mandm
Re: Posting XML data to aspx script using PHP
« Reply #5 on: July 20, 2010, 03:34:30 PM »
So may be its me that i am doing something wrong or i do not understand the curl concepts yet, let me ask you in detail


$year
=date('Y');
$month=date('m');
$day=date('d');

$xml = file_get_contents("http://e-edition.metro.co.uk/$year/$month/$day/edition.xml");

This part is where the latest XML data is read by the php code into a variable

$ch = curl_init(); //initiate the curl session
curl_setopt($ch, CURLOPT_URL, "http://edition.pagesuite-professional.co.uk/create_pdf.aspx?");

///here you set the URL to post to so i have a similar aspx URL i should be putting that here?

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // tell curl to return data in a variable
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // post the xml
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // set timeout in seconds
$xmlResponse = curl_exec ($ch);
curl_close ($ch);

$start=strpos($xmlResponse, "http");
$url=substr($xmlResponse, $start);
$end=strpos($url, ".pdf");
$url=substr($url, 0, $end+4);

This $xmlResponse is the response from the receiving server that it sends back to our code
and in the later code you just create a pdf file downloadable for this xml

echo
"Click here to download the Metro";
?>

so few questions that i have are, my XML is


     
       
          John Doe
         

     


is this the same format as the news paper XML, and can this be used to post using curl with all the same parameters?
secondly, how can i test the receiving script on my server? can you point me to a receiving post script?

All these answers will definetely help me a lot