0

I am using curl and xml to post my xml to a URL.

I am getting response from it right now, but its not displaying in output.

when I take the View Source page by right clicking its there like the XML file output.

I need to store that xml document to a file.

I used this code below

<?
$path = "https://newport.ingrammicro.com/mustang"; //Relative path to the file with $_POST parsing
$ch = curl_init($path); //Initialise curl resource with above file
$data = "my xml document to post"; 
//Data to be sent to the file

curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //Send the data to the file?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
curl_setopt($ch, CURLOPT_HEADER, 0);
$val = curl_exec($ch);   
curl_close($ch); //Close curl session
$fp = fopen('data.xml', 'w');
fwrite($fp, $val);
fclose($fp);
?>

but when I opened the saved file only a single value "1" this is the value I'm getting in the file.

actually its the true value from the curl statement($val = curl_exec($ch); ) bool(true) i tried to echo the value of Sval , it also "1"

How can i save my XML output file exactly as the XML file output.when i take view page source

it will look like this

<PNAResponse>
  <Version>2.0</Version>
  <TransactionHeader>
    <SenderID>14-018111</SenderID>
    <ReceiverID>14-018111</ReceiverID>
    <ErrorStatus ErrorNumber=""/>
    <DocumentID>{F1B17BBB-F848-4693-914A-B6943AA9009A}</DocumentID>
    <TransactionID/>
    <TimeStamp>2009-06-17T22:42:44</TimeStamp>
  </TransactionHeader>
  <PriceAndAvailability SKU="V06669" Quantity="5" GovtProgramType="PA" GovtEndUserType="F">
    <Price>1413.04</Price>
    <SpecialPriceFlag>Y</SpecialPriceFlag>
    <ManufacturerPartNumber>V11H270020</ManufacturerPartNumber>
    <ManufacturerPartNumberOccurs/>
    <VendorNumber>4913</VendorNumber>
    <Description>POWERLITE 1735W LCD PROJ</Description>
    <ReserveInventoryFlag>N</ReserveInventoryFlag>
    <AvailableRebQty>0</AvailableRebQty>
    <Branch ID="10" Name="Mira Loma, California">
      <Availability>7</Availability>
      <OnOrder>8</OnOrder>
      <ETADate>2009-06-19</ETADate>
    </Branch>
    <Branch ID="30" Name="Millington, Tennessee">
      <Availability>0</Availability>
      <OnOrder>4</OnOrder>
      <ETADate>2009-06-22</ETADate>
    </Branch>
    <Branch ID="40" Name="Carol Stream, Illinois">
      <Availability>0</Availability>
      <OnOrder>18</OnOrder>
      <ETADate>2009-06-18</ETADate>
    </Branch>
    <Branch ID="80" Name="Jonestown Pennsylvania">
      <Availability>0</Availability>
      <OnOrder>9</OnOrder>
      <ETADate>2009-06-23</ETADate>
    </Branch>
  </PriceAndAvailability>
</PNAResponse>

Does anyone have an idea how to do this?

1
  • 1
    Next time you ask a question here(or anywhere else), please try to remove unnecessary filler and information you are absolutely sure is not needed, for example the XML document here. Also keep in mind that paragraphs and line breaks should not always match. Commented Jun 18, 2009 at 6:16

4 Answers 4

1

Set CURLOPT_RETURNTRANSFER to true:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Why did you set it to 0 in the first place?

Sign up to request clarification or add additional context in comments.

Comments

1

The curl_exec() function returns TRUE on success and FALSE on failure, unless you set the CURLOPT_RETURNTRASFER setting to true (1):

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

This will cause the curl_exec() function to return the response body of the request rather than a true/false value.

See also the 'Return Values' section at PHP: curl_exec - Manual

Comments

1

also if you want try adding some user agent to be sure you are getting the same output as a real browser

                @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");

Comments

0

Like Grabiel Sosa said, "you may want to try adding some user agent to be sure you are getting the same output as a real browser".

The website http://www.user-agents.org/index.shtml?moz contains tons of User-Agent (Mozilla and others)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.