?
Solved

help make SOAP-encapsulated Web Service https post faster

Posted on 2007-03-28
5
Medium Priority
?
296 Views
Last Modified: 2013-12-25
I use the following code to post form information to a client's server.  It works, but it is extremely slow and I'm afraid that we may be losing customers because of how long they have to wait before the form submits.  I would really appreciate it if any of you wonderful experts could take a look and tell me if there is anything that I could change to make it faster.  Thanks.

/* This function is used for HTTPS posts. */
function pc_post_request($host,$url,$content='')
{
      $timeout = 2;

      // create a new XML document
      $doc = domxml_new_doc('1.0');

      $envelope = $doc->create_element('soap:Envelope');
      $envelope = $doc->append_child($envelope);
      $envelope->set_attribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
      $envelope->set_attribute('xmlns:xsd','http://www.w3.org/2001/XMLSchema');
      $envelope->set_attribute('xmlns:soap','http://schemas.xmlsoap.org/soap/envelope/');

      $sbody = $doc->create_element('soap:Body');
      $sbody = $envelope->append_child($sbody);

      $root = $doc->create_element('ProcessLead');
      $root = $sbody->append_child($root);
      $root->set_attribute('xmlns','http://url that I can not post here');

      $outer = $doc->create_element('lead');
      $outer = $root->append_child($outer);

      // add a child node for each parent field
      foreach ($content as $fieldname => $fieldvalue)
      {
        $child = $doc->create_element($fieldname);
        $child = $outer->append_child($child);
        $child->set_attribute('xmlns', '');
        $value = $doc->create_text_node($fieldvalue);
        $value = $child->append_child($value);
      } // foreach
      // get completed xml document
      $xml_string = $doc->dump_mem(true);
      
      $content_length = strlen($xml_string);
      $request_body = "POST $url HTTP/1.1\r\n" .
      "Host:" . $host ."\r\n" .
      "Content-type:" . " text/xml; charset=utf-8\r\n" .
      "Content-length:" . $content_length . "\r\n" .
      "SOAPAction:" . "\"http://url that I can not post here\"" . "\r\n\r\n" .

      $xml_string;

      $sh = fsockopen("ssl://".$host,443,&$errno,&$errstr,$timeout)
            or die("Can't open socket to $host: $errno $errstr");

      fputs($sh,$request_body);
      $response = '';
      while(! feof($sh))
      {
            $response .= fread($sh,16384);
      }
      fclose($sh) or die("Can't close socket handle: $php_errormsg");

      list($response_headers,$response_body) = explode("\r\n\r\n",$response,2);
      $response_header_lines = explode("\r\n",$response_headers);

      //first line of headers is the HTTP response code
      $http_response_line = array_shift($response_header_lines);
      if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line,$matches))
      {
            $response_code = $matches[1];
      }

      //put the rest of the headers in an array
      $response_header_array = array();
      foreach ($response_header_lines as $header_line)
      {
            list($header,$value) = explode(': ',$header_line,2);
            $response_header_array[$header] = $value;
      }
      return array($response_code,$response_header_array,$response_body);
}
0
Comment
Question by:Ima Jedi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 18808854
Code seems ok. I gues the server you are contacting is the slow part. att timing  around the reading of the server-request:

$start = microtime(true);
      $sh = fsockopen("ssl://".$host,443,&$errno,&$errstr,$timeout)
            or die("Can't open socket to $host: $errno $errstr");

      fputs($sh,$request_body);
      $response = '';
      while(! feof($sh))
      {
            $response .= fread($sh,16384);
      }
      fclose($sh) or die("Can't close socket handle: $php_errormsg");
$timeYouCanNotInfulence = microtime(true)-$start;

Which values do you get for $timeYouCanNotInfulence when running your script
0
 

Author Comment

by:Ima Jedi
ID: 18809213
I get '-0.146131' for $timeYouCanNotInfulence.  What does that mean?
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 18809684
It means it took 0.14 Seconds to connect and process your request on the foreing server. Fast enough i would say. Its negative because I substracted the end from the start-time. Should be vice versa
$timeYouCanNotInfulence = $start - microtime(true);
to ge a positive value.

So I guess there must be another bottle neck. Maybe you can add such time measure points to the part before and after the connection to get a better view where the bottleneck in your code is.
0
 

Author Comment

by:Ima Jedi
ID: 18810349
Thank you very much hernst42.  That is a great tip and I'll definitely use it to figure out where the problem is.
0
 

Author Comment

by:Ima Jedi
ID: 18811235
In case anyone needs this answer...it turns out that our server doesn't like to do calculations with the call to microtime() or time() used as one of the values (a setting maybe?).  So the number I was getting back by using $timeYouCanNotInfulence = microtime(true)-$start; was incorrect.  I changed it so that I was storing the start time and the end time in two separate variables (I used time() instead of microtime()).  Then I subtracted the start time from the end time and learned that it was actually taking 132 seconds to connect to the client's server and send the information.  So...in the end I learned that there is nothing I can do to speed it up except to ask our client to have their IT team look into it.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question