Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/99248-api-delaying-on-my-endany-ideas/
Share on other sites

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

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??

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);

?>

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;
}

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;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.