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(arra y(
'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.X MLHTTP");
} 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.onreadystatecha nge = functionname;
AJAXObject.send(null);
}
}
function testScript(){
var url = "test.php";
ajaxcall(url,checkTestScri pt);
}
function checkTestScript(){
if(AJAXObject.readyState == 4){
alert(AJAXObject.responseT ext);
}
}
</script>
</head>
<body>
<input type='button' onClick="testScript();" value="Click Here for Test"> Please Click The Button
</body>
</html>
//options
$ctx = stream_context_create(arra
'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.X
} 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.onreadystatecha
AJAXObject.send(null);
}
}
function testScript(){
var url = "test.php";
ajaxcall(url,checkTestScri
}
function checkTestScript(){
if(AJAXObject.readyState == 4){
alert(AJAXObject.responseT
}
}
</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";
}
?>
Have you checked the allow_url_fopen directive is turned on in your INI settings?
ASKER
It is on
Yeah, my bad just reread your post - I misread your question. My apologies.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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"
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
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.
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.
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
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
This page might have something useful...
http://www.php.net/manual/en/function.curl-setopt.php
http://www.php.net/manual/en/function.curl-setopt.php
ASKER
I can't give it out because it is a bank site with account info.
You can't give us a test site? How can we test?
ASKER
I added
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
to you script and it worked.
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
to you script and it worked.
ASKER
Thank you for educating me in the art of CURL
Great! Congratulations. I'll have to see if it works with non-SSL sites, then if so I can leave that in the code.
Yes, that line has no effect on non-HTTPS sites. Thanks for sharing! ~Ray