Avatar of jmsloan
jmsloan
 asked on

file_get_content timeout with ajax

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

?>

Open in new window

ProgrammingAJAXPHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Derokorian

Have you checked the allow_url_fopen directive is turned on in your INI settings?
jmsloan

ASKER
It is on
Derokorian

Yeah, my bad just reread your post - I misread your question. My apologies.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jmsloan

ASKER
I get the following error when I try to use the CURL Function above

CURL FAIL: https://mytestwebsite.com:8443/testdir/webAuth.do?id=1234acct=1234?s=lulu&f=snl1c1ohgvt1 TIMEOUT=2, CURL_ERRNO=60array(19) { ["url"]=> string(147) "https://mytestwebsite.com:8443/testdir/webAuth.do?id=1234acct=1234?s=lulu&f=snl1c1ohgvt1" ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(-8179) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0.294436) ["connect_time"]=> float(0.339184) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) } NO https://mytestwebsite.com:8443/testdir/webAuth.do?id=1234acct=1234


Does it matter that I'm actually connecting with https: to a specific port and using GET variables?

https://mytestwebsite.com:8443/testdir/webAuth.do?id=1234&acct=1234
jmsloan

ASKER
I guess my question is.  

What is the best way to send a GET request via https to a specific port and echo the response?  Also a timeout of no more than 10 seconds must be in included.

Ray Paseur

The curl error #60 appears to be related to the use of SSL.
http://php.net/manual/en/function.curl-errno.php

What is the actual URL of your test script on the HTTPS site?  I'll try to read it and show you my code.  Thanks, ~Ray
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

This page might have something useful...
http://www.php.net/manual/en/function.curl-setopt.php
jmsloan

ASKER
I can't give it out because it is a bank site with account info.
Ray Paseur

You can't give us a test site?  How can we test?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
jmsloan

ASKER
I added

curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );

to you script and it worked.
jmsloan

ASKER
Thank you for educating me in the art of CURL
Ray Paseur

Great!  Congratulations.  I'll have to see if it works with non-SSL sites, then if so I can leave that in the code.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

Yes, that line has no effect on non-HTTPS sites.  Thanks for sharing! ~Ray