jaxdevil Posted April 2, 2008 Share Posted April 2, 2008 I made an API PLugin for Freightquote.com , it works, the information is submited to the script via a form, the form fields populate into an XML, which is sent to a remote server via fsockopen, the data is processed at the remote server, new XML data is sent back, and is displayed. The problem is it is taking a LONG time for the program to complete its 'run'. The remote server sends the data back within 4 or 5 seconds, but it takes my script about two and a half minutes to complete its run. Is there a way to close the transmission upon receipt of data? Or to control the length of time the transmission takes place? Here is a link to the script in action... http://www.personpic.com/xmlform1.php Here is my code... <?php $email='xxx@xxx.com'; $password='xxx'; $billto='SHIPPER'; $orig_zipcode=$_POST['orig_zipcode']; $dest_zipcode=$_POST['dest_zipcode']; $weight=$_POST['weight']; $class=$_POST['class']; $productdesc=$_POST['productdesc']; $pieces=$_POST['pieces']; $stackable=$_POST['stackable']; $trackback=$_POST['trackback']; $fp = fsockopen("b2b.freightquote.com", 80, $errno, $errstr, 30); if (!$fp) { $die('Could not open connection.'); } else { $xmlpacket = "<FREIGHTQUOTE REQUEST=\"QUOTE\" EMAIL=\"$email\" PASSWORD=\"$password\" BILLTO=\"$billto\"> <ORIGIN> <ZIPCODE>$orig_zipcode</ZIPCODE> </ORIGIN> <DESTINATION> <ZIPCODE>$dest_zipcode</ZIPCODE> </DESTINATION> <SHIPMENT> <WEIGHT>$weight</WEIGHT> <CLASS>$class</CLASS> <PRODUCTDESC>$productdesc</PRODUCTDESC> <PIECES>$pieces</PIECES> <STACKABLE>$stackable</STACKABLE> </SHIPMENT> </FREIGHTQUOTE>"; $contentlength = strlen($xmlpacket); $out = "POST /dll/FQXMLQuoter.asp HTTP/1.0\r\n"; $out .= "Host: b2b.freightquote.com\r\n"; $out .= "Connection: Keep-Alive\r\n"; $out .= "Content-type: text/xml\r\n"; $out .= "Content-length: $contentlength\r\n\r\n"; $out .= $xmlpacket; fwrite($fp, $out); while (!feof($fp)) { $theOutput .= fgets($fp, 128); } fclose($fp); preg_match('/QUOTEID="(.*?)"/',$theOutput,$quoteid); preg_match('/<CARRIERNAME>(.*?)<\/CARRIERNAME>/',$theOutput,$carriername); preg_match('/<SCAC>(.*?)<\/SCAC>/',$theOutput,$scac); preg_match('/<RATE>(.*?)<\/RATE>/',$theOutput,$rate); preg_match('/<FREIGHTCOST>(.*?)<\/FREIGHTCOST>/',$theOutput,$freightcost); preg_match('/<FUEL_SURCHARGE>(.*?)<\/FUEL_SURCHARGE>/',$theOutput,$fuel_surcharge); preg_match('/<TRANSIT>(.*?)<\/TRANSIT>/',$theOutput,$transit); $quoteid=$quoteid[1]; $carriername=$carriername[1]; $scac=$scac[1]; $rate=$rate[1]; $freightcost=$freightcost[1]; $fuel_surcharge=$fuel_surcharge[1]; $transit=$transit[1]; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>XML Request</title> </head> <body> <form action="xmlform1.php" method="post"> <fieldset> <label></label> <input name="trackback" type="hidden" value="<?php print $trackback; ?>"/> <input name="quoteid" type="hidden" value="<?php print $quoteid; ?>"/> <input name="carriername" type="hidden" value="<?php print $carriername; ?>"/> <input name="scac" type="hidden" value="<?php print $scac; ?>"/> <input name="rate" type="hidden" value="<?php print $rate; ?>"/> <input name="freightcost" type="hidden" value="<?php print $freightcost; ?>"/> <input name="fuel_surcharge" type="hidden" value="<?php print $fuel_surcharge; ?>"/> <input name="transit" type="hidden" value="<?php print $transit; ?>"/> <input type="submit" value="Continue"/> </fieldset> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/ Share on other sites More sharing options...
gluck Posted April 2, 2008 Share Posted April 2, 2008 Use break points to debug your code. (print output and see). Time your script. Have a timer start when the script starts and check the time at each break point. Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-507814 Share on other sites More sharing options...
jaxdevil Posted April 2, 2008 Author Share Posted April 2, 2008 Sorry, don't know what you mean. Can you help me with an example? Also, remember this is a multipart script, it loads, sends data (which is does right off the bat) the data is received back, which happens within a few seconds, and then it hangs, so it would be at the bottom. SK Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-507824 Share on other sites More sharing options...
jaxdevil Posted April 3, 2008 Author Share Posted April 3, 2008 The last several post I have made no one can figure them out. I know I am not the best one at PHP on here so whats the deal? This has got to be something simple with the fsockopen staying open for so long. There has to be a way to close it once the data is received?? Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-508026 Share on other sites More sharing options...
uniflare Posted April 3, 2008 Share Posted April 3, 2008 Sorry, don't know what you mean. Can you help me with an example? Also, remember this is a multipart script, it loads, sends data (which is does right off the bat) the data is received back, which happens within a few seconds, and then it hangs, so it would be at the bottom. SK he means to use microtime(), before and after the portion of code you would like to time, subtract the first from the second and you have a split-second timer eg: <?php $timer[] = implode(".",explode(" ",microtime())); $fp = fsockopen("b2b.freightquote.com", 80, $errno, $errstr, 30); if (!$fp) { $die('Could not open connection.'); } else { $xmlpacket = "<FREIGHTQUOTE REQUEST=\"QUOTE\" EMAIL=\"$email\" PASSWORD=\"$password\" BILLTO=\"$billto\"> <ORIGIN> <ZIPCODE>$orig_zipcode</ZIPCODE> </ORIGIN> <DESTINATION> <ZIPCODE>$dest_zipcode</ZIPCODE> </DESTINATION> <SHIPMENT> <WEIGHT>$weight</WEIGHT> <CLASS>$class</CLASS> <PRODUCTDESC>$productdesc</PRODUCTDESC> <PIECES>$pieces</PIECES> <STACKABLE>$stackable</STACKABLE> </SHIPMENT> </FREIGHTQUOTE>"; $contentlength = strlen($xmlpacket); $out = "POST /dll/FQXMLQuoter.asp HTTP/1.0\r\n"; $out .= "Host: b2b.freightquote.com\r\n"; $out .= "Connection: Keep-Alive\r\n"; $out .= "Content-type: text/xml\r\n"; $out .= "Content-length: $contentlength\r\n\r\n"; $out .= $xmlpacket; fwrite($fp, $out); while (!feof($fp)) { $theOutput .= fgets($fp, 128); } fclose($fp); $timer[] = implode(".",explode(" ",microtime())); $timer = $timer[1] - $timer[0]; print_r($timer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-508033 Share on other sites More sharing options...
jaxdevil Posted April 3, 2008 Author Share Posted April 3, 2008 That told me it took 0.686279 seconds to process. So why does it take the page 153 seconds to load? Thanks, SK Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-508169 Share on other sites More sharing options...
uniflare Posted April 3, 2008 Share Posted April 3, 2008 move the timer around, time half the script etc, narrow down the culprit Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-508689 Share on other sites More sharing options...
jaxdevil Posted April 3, 2008 Author Share Posted April 3, 2008 Ok, I moved it down just arround the form area and I get the timer saying "5.6E-05" I didn't know there were alphabets in the timeline? SK Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-508712 Share on other sites More sharing options...
uniflare Posted April 4, 2008 Share Posted April 4, 2008 shouldnt really, sounds like some math error, try: print_r($timer); $timer = $timer[1] - $timer[0]; print_r($timer); Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-509402 Share on other sites More sharing options...
jaxdevil Posted April 4, 2008 Author Share Posted April 4, 2008 The remote server is not sending an EOF data. I need to incorporate something like the following script (which I found looking for a solution) into my script, to make it time out after receiving a certain amount of data, or after data is done being received. Any ideas how to incorporate this into my script.... function readDataStream(&$fp) { //variable declarations $line = null; $inHead = false; $xmlResponse = ''; $this->writeLog("\n\nStartReadStream"); //set time-out stream_set_timeout($fp, $this->fsockopenTimeout); //get the response $lineNum = 0; while ( !feof($fp) ) { $lineNum++; $status = stream_get_meta_data($fp); if ( !$fp || $status['timed_out']) { //if for some reason the connection is lost, stop right away $this->writeLog("\nConnection lost or timed out"); break; } if ( $lineNum>500 ) { //don't let it run forever, line limit here $this->writeLog("\nLine count over 500, stopping"); break; } $line = fgets($fp, 512); $line = trim($line); $this->writeLog("\nReceived line $lineNum:\n".$line); $fullResponse.=$line; if ( strpos( $line ,"HTTP/1")!==false) { $inHead = true; } elseif ( trim($line)=='') { //empty line marks the end of head $inHead = false; continue; } if ( $inHead ) { $this->writeLog("\n(In head)"); } else { $xmlResponse.= trim($line); } if ( strpos($line,"</ewayResponse>")!==false) { //this way we forcefully end the reading not waiting for the eof which may never come $this->writeLog("\nLast line reached, stopping"); break; } } $this->writeLog("\nReceived full: \n".$fullResponse); $this->writeLog("\nxml: $xmlResponse"); return $xmlResponse; } Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-509543 Share on other sites More sharing options...
benjaminbeazy Posted April 5, 2008 Share Posted April 5, 2008 that's too much code for me to look through. inside your while (!feof($fp)), throw in a $var = time(); and test it against the start time of the read, if its greater than however many seconds: throw a notice of some sort and..... here it is: break; The remote server is not sending an EOF data. I need to incorporate something like the following script (which I found looking for a solution) into my script, to make it time out after receiving a certain amount of data, or after data is done being received. Any ideas how to incorporate this into my script.... function readDataStream(&$fp) { //variable declarations $line = null; $inHead = false; $xmlResponse = ''; $this->writeLog("\n\nStartReadStream"); //set time-out stream_set_timeout($fp, $this->fsockopenTimeout); //get the response $lineNum = 0; while ( !feof($fp) ) { $lineNum++; $status = stream_get_meta_data($fp); if ( !$fp || $status['timed_out']) { //if for some reason the connection is lost, stop right away $this->writeLog("\nConnection lost or timed out"); break; } if ( $lineNum>500 ) { //don't let it run forever, line limit here $this->writeLog("\nLine count over 500, stopping"); break; } $line = fgets($fp, 512); $line = trim($line); $this->writeLog("\nReceived line $lineNum:\n".$line); $fullResponse.=$line; if ( strpos( $line ,"HTTP/1")!==false) { $inHead = true; } elseif ( trim($line)=='') { //empty line marks the end of head $inHead = false; continue; } if ( $inHead ) { $this->writeLog("\n(In head)"); } else { $xmlResponse.= trim($line); } if ( strpos($line,"</ewayResponse>")!==false) { //this way we forcefully end the reading not waiting for the eof which may never come $this->writeLog("\nLast line reached, stopping"); break; } } $this->writeLog("\nReceived full: \n".$fullResponse); $this->writeLog("\nxml: $xmlResponse"); return $xmlResponse; } Quote Link to comment https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/#findComment-509823 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.