views:

268

answers:

3

Im currently working with an API which requires we send our collection details in xml to their server using a post request.

Nothing major there but its not working, so I want to output the sent xml to a txt file so I can look at actually whats being sent!!

Instead of posting to the API im posting to a document called target, but the xml its outputting its recording seems to be really wrong. Here is my target script, note that the posting script posts 3 items, so the file being written should have details of each post request one after the other.

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

// get the request data...
$payload = '';
$fp = fopen('php://input','r');
$output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
    $payload .= fgets($fp);
    fwrite($output_file, $payload);
}

fclose($fp);
fclose($output_file);
?>

I also tried the following, but this just recorded the last post request so only 1 collection item was recorded in the txt file, instead of all 3

output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
    $payload .= fgets($fp);
}
fwrite($output_file, $payload);
fclose($fp);
fclose($output_file);

I know im missing something really obvious, but ive been looking at this all morning!

A: 

you should probably use curl instead to fetch the content and then write it via fwrite

dusoft
A: 

change

  $payload .= fgets($fp);

to

 $payload = fgets($fp);
ts
A: 

Change

$output_file = fopen('output.txt', 'w');

to

$output_file = fopen('output.txt', 'a');

Also, change

$payload .= fgets($fp);

to

$payload = fgets($fp);
Zeta Two
or put fwrite($output_file, $payload); right after the while loop, not inside.
Alex