troubleshooting Question

file_get_content timeout with ajax

Avatar of jmsloan
jmsloan asked on
ProgrammingPHPAJAX
14 Comments1 Solution552 ViewsLast Modified:
I have a test script that connects to a webpage via ajax and returns a response.  All is working accept if the network is down.  The timeout takes to long to return a response.  I would like to manually dial the timeout to 10 seconds instead of the default.  Below is my test.html page.  Attach is my php script that gets called by ajax.    I have tried adding option to the file_get_contents but is doesn't seem to work.

//options
$ctx = stream_context_create(array(
    'http' => array(
        'timeout' => 5,
        'ignore_errors' => true
        )
    )
);
//////////////////////


<html>
<head>
function getXmlHttpRequestObject() {
   if (window.XMLHttpRequest) {
      return new XMLHttpRequest();
   } else if(window.ActiveXObject) {
      return new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      alert("Please upgrade your browser to gain full functionality.");
   }
}
 
//Our XmlHttpRequest object
var AJAXObject = getXmlHttpRequestObject();
 
function ajaxcall(url,functionname){
   if (AJAXObject.readyState == 4 || AJAXObject.readyState == 0) {
      AJAXObject.open("GET", url, true);
      AJAXObject.onreadystatechange = functionname;
      AJAXObject.send(null);
   }
}
 
function testScript(){
  var url = "test.php";
  ajaxcall(url,checkTestScript);
}
 
function checkTestScript(){
   if(AJAXObject.readyState == 4){
      alert(AJAXObject.responseText);
   }
}
 
</script>
 
</head>
<body>
<input type='button' onClick="testScript();" value="Click Here for Test"> Please Click The Button
</body>
</html>
<?php

$url = "https://www.testwebsite/";
 
$str = file_get_contents($url);
 
$field1 = substr($str,0,strpos($str," "));
$field2 = substr($str,strpos($str," ")+1);
 
if (preg_match("/APPROVED/i", "$str")) {
   $field1 = substr($str,0,strpos($str," "));
   $field2 = substr($str,strpos($str," ")+1);
   echo "1|$field1|$field2";
}else{
   echo "0|$str";
}

?>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 14 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros