Read from file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SSIBill
Forum Newbie
Posts: 5
Joined: Fri Jun 04, 2010 9:04 am

Read from file

Post by SSIBill »

Hello all, This is my first time here and hope to get settled in for a long run. I'm also really just playing with PHP for the first time in a long time. My PHP is weak as you'll see.

Anyway, I have this strange issue that I hope to find help for.

I'm creating a script and I need to read and parse out a text file. The process here is this:
I do and HTML post to a vendors website (I know, HTML post isn't favorable but I'm told it's all the vendor has available) and get back a result that contains (in test mode) 3 sets of UPS tracking numbers, rates and such. No. I'm not getting this info from UPS. The vendor is actually United Stationers. All that I have working and the "Success/Fail" result is being stored in a temporary text file. This is where I'm having a problem. I know need to read that text file. Here's the contents of that file:
SUCCESS<BR><PRE>1999-11-23327
1 ~UPSS~1Z2Z3Z4Z5Z6Z7Z8Z9Z~DF~19991122~UPSN~A ~999999001~999999001~003~03XXXFF ~1070990 ~
2 ~00002.93~000.89~00000.00~00000.00~
3 ~000.96~0000003.82~
1 ~UPSS~1ZZ2ZZ3ZZ4ZZ5ZZ6ZZ~DF~19991122~UPSN~A ~999999001~999999001~004~04YYYQY ~1070991 ~
2 ~00003.07~000.89~00000.00~00000.00~
3 ~002.66~0000003.96~
1 ~UPSS~1ZZZ2ZZZ3ZZZ4ZZZ5Z~DF~19991122~UPSN~A ~999999001~999999001~005~05ZZZ4C ~1070992 ~
2 ~00003.07~000.89C00000.00~00000.00~
3 ~002.92~0000003.96~
EOF0000009 </PRE>

I've tried just about every function I can find and none seem to be able to handle the data.

I'm currently trying this without success.

$file = fopen("temp1.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
// var_dump($file);
}
fclose($file);

but I've also tried using file_get_contents without success.

Any pointers would be greatly appreciated.

Bill
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Read from file

Post by phdatabase »

Here's the questions that come to mind.

Do you get a POST array back or just the string you show.
Are you only trying to read the file into a variable or are you trying to parse it?
Assuming the first, why won't file_get_contents work?
Error/Warning messages?
SSIBill
Forum Newbie
Posts: 5
Joined: Fri Jun 04, 2010 9:04 am

Re: Read from file

Post by SSIBill »

No, I get an empty string, string(0) or just a blank space if I echo or print it. I ultimately need to parse out the values but I'll be happy for now if I can just figure why I can't seem to get it in a string. If I use strlen() on it it returns 0, if I use is_string() it returns false. I dunno what to do!! No error messages at all.
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Read from file

Post by phdatabase »

All that would imply the string never gets stored in the first place. If you say there are no error messages, then I would presume the file gets created but nothing else. How do you write the file?
SSIBill
Forum Newbie
Posts: 5
Joined: Fri Jun 04, 2010 9:04 am

Re: Read from file

Post by SSIBill »

// ************************** REMOVE THIS AFTER TESTING ***********
$strServer='TST.MAILBOX'; // 1st value// TST.MAILBOX
$strUID='SAMPLES'; // 2nd value// SYSTMSOL
$strPwd='SAMPLES9'; // 3rd value// AB13FZB8
$strSubmit='GET'; // 4rd value// use LIST to get all files
$strFile='UPSTRACK'; // 5rd value// $strFile='WELCOME'
$strFolder=''; // 6rd value
$strType='ASCII'; // 7rd value
$strTransaction=''; // 8rd value
$strLocalFile=''; // 9rd value
$strRequestType=3; // 10rd value
// ******************************************************************
// turn test mode on and off
$intIsLive = 0; // 0 = Test, 1 = Live
//extract data from the post
extract($_POST);
//set create and set default string
$fields_string = '';

// set url to variable
if($intIsLive == 0)
$url = "https://stage-elink2.testlink.com/access/services.asp"; // Test Inviroment
elseif ($intIsLive == 1)
$url = "https://elink2.livelink.com/access/services.asp"; // Live Inviroment

$fields = array(
'txtServer'=>urlencode($strServer),
'txtUID'=>urlencode($strUID),
'txtPwd'=>urlencode($strPwd),
'cmdSubmit'=>urlencode($strSubmit),
'srcFile'=>urlencode($strFile),
'srcFolder'=>urlencode($strFolder),
'txtType'=>urlencode($strType),
'RequestType'=>urlencode($strRequestType)
// 'RequestTransmission'=>urlencode($strTransaction),
// 'LocalFile'=>urlencode($strLocalFile)
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { "+".$fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
$fields_string = $fields_string."+";
//print rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

$tempFile = fopen("/curl/download/scripts/rpm/temp1.txt", "w");
//$tempFile = fopen("/curl/download/scripts/rpm/eLINK.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $tempFile);
curl_setopt($ch, CURLOPT_HEADER, 0);

$result = curl_exec($ch);
curl_close($ch);
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Read from file

Post by phdatabase »

Read the FAQs or contact support.
SSIBill
Forum Newbie
Posts: 5
Joined: Fri Jun 04, 2010 9:04 am

Re: Read from file

Post by SSIBill »

?? What FAQ's and WHAT support??
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

Re: Read from file

Post by phdatabase »

This is your script? You wrote it? You didn't download it or find it in a book?

I don't do support for other developer's scripts.
SSIBill
Forum Newbie
Posts: 5
Joined: Fri Jun 04, 2010 9:04 am

Re: Read from file

Post by SSIBill »

nope. I wrote it with the help of the php function list.

don't expect support but was hoping for help figuring this out.
Post Reply