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
#
$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);
?>