# Still seeking advice

**URL:** <https://forum.kirupa.com/t/still-seeking-advice/293628>\
**Category:** programming\
**Created:** [July 31, 2009, 2:36pm UTC](https://forum.kirupa.com/t/still-seeking-advice/293628 "2009-07-31T14:36:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![alextrevino](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@alextrevino](https://forum.kirupa.com/u/alextrevino)\
**Post date:** [July 31, 2009, 2:36pm UTC](https://forum.kirupa.com/t/still-seeking-advice/293628/1 "2009-07-31T14:36:43Z")

</div>

In performing some further research into the matter I realized that I was trying to write a file that didn’t exist. Here is the new code that works great, but I still need some assistance in appending the written file. I’ve tried all variations of the fopen() (e.g. a,a+, w, etc…) I do not want my inventory to get bloated, so when a person checks the inventory I need the xml file written over with a fresh ‘check of the inventory’.

```php

 
<?php 
#
   $path = "/cdmapi/dlapi.asmx/DLAPI?Request=retail_inv&DealerID=4229&LoginID=lrsa&Password=cdmd@ta";
   $port = 80;
   $host = "api.cdmdata.com";
 
   if (count($argv) > 1) $path = $argv[1];
   if (count($argv) > 2) $port = $argv[2];
   if (count($argv) > 3) $host = $argv[3];
   $address = gethostbyname($host);
   $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
   socket_connect($socket, $address, $port);
   $in = "GET $path HTTP/1.0

";
   socket_write($socket, $in, strlen($in));
   while ($data = socket_read($socket, 2048)) {
      //echo $out;
 
  $file = "inventory.xml";
  $content = $data;
 
  if (is_writable($file)){
 
   if (!$handle = fopen($file, 'a')){
    echo "Cannot open file ($file)";
    exit;
   }
 
   if (fwrite($handle, $content) === FALSE) {
    echo "Cannot write to ($file)";
    exit;
   }
 
   echo "Success, wrote ($content) to file ($file)";
 
   fclose($handle);
 
  } else {
   echo "the file $file is not writable";
   }
}
   socket_close($socket);
 
?>

```
