Hi guys... i would like to ask on how do i set a timeout request in php? what i mean is im creating a script where i send a query and a request from different website. Example i have a website like www.mywebsite.com will request a content or a service from www.givingservice.com and assuming that my header content is asking for a certain service or value... and i have this code to get the value that i want from the www.givingservice.com
function do_post_request($url, $data, $request,$optional_headers = null){
$params = array('http' => array('method' => 'POST', 'content' => $data));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
return "error";
}
$response = @stream_get_contents($fp);
if ($response === false) {
return "error";
}
$res = mysql_query("insert into mytable values('thevalues')");
return $response;
}
As you can see in my code the
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
im passing my content in $ctx and try to ask for reply from $url... so im expecting a string reply from the url if the connection was successful. but the problem is when i try to send the request to that website and during that request i encountered a network traffic i dont get any reply... and the code automatically execute this content
if (!$fp) {
return "error";
}
I tried contacting the website that sending the reply but they said that they send the reply just fine. im guessing is that i encountered a network problem thats why i didnt get the reply on time or as expected. so they suggested that i need to create a script that send the same request but gives a request timeout or a time for them to reply. meaning a script that will really wait for their reply for a certain time like let say 20-40seconds. but the problem now is i dont know how to do it... i tried using this code
$fp = @fsockopen($url,443,$errno,$errstr,20);
as you an see i used fsockopen instead of fopen for me to be able to have a time out request for 20seconds but the problem is i cant figure out how do i pass the request string that i need to pass using fsockopen. from what i understand from my fsockopen code it just try to see if there is a reply from the $url within 20seconds. but it does not send any string or request string for processing...
so heres what i want...
1.) Try to send a request string from $url with a certain query string to be passed on the header
2.) wait for the reply of $url with a certain period of time like 20seconds
3.) process that reply properly...
Hope you can help me with my problem guys... thanks in advance