如何在PHP发送头中设置等待超时 [英] How to set wait timeout in php send header

查看:73
本文介绍了如何在PHP发送头中设置等待超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...我想问一下如何在php中设置超时请求?我的意思是即时通讯创建了一个脚本,在该脚本中我发送了来自不同网站的查询和请求.例如,我有一个像www.mywebsite.com这样的网站,它将从www.givingservice.com请求内容或服务,并假设我的标题内容正在要求某种服务或价值...并且我有此代码来获取价值我想从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);		
		
		//$fp = @fsockopen($url,443,$errno,$errstr,20);		
		if (!$fp) {
			return "error";					
		}
		$response = @stream_get_contents($fp);		
		if ($response === false) {
			return "error";		
		}
                $res = mysql_query("insert into mytable values(''thevalues'')");
		return $response;			
}



您可以在我的代码中看到

  $ ctx  = stream_context_create($ params);
 $ fp  = @fopen($ url,'  rb' ,false,$ ctx);



即时通讯在$ ctx中传递我的内容,并尝试要求$ url答复...因此,如果连接成功,即时通讯期望从url进行字符串答复.但是问题是当我尝试将请求发送到该网站时,在该请求期间我遇到了网络流量,但没有得到任何答复...并且代码自动执行了此内容

 如果(!$ fp){
    返回 " ;
}



我尝试联系发送答复的网站,但他们说他们发送答复就很好.我猜是我遇到了网络问题,这就是为什么我没有按时或按预期得到答复.所以他们建议我需要创建一个脚本,该脚本发送相同的请求,但给出请求超时或他们答复的时间.意思是一个脚本,该脚本将真正等待他们的答复特定时间,例如20-40秒.但是现在的问题是我不知道该怎么做...我尝试使用此代码

$fp = @fsockopen($url,443,$errno,$errstr,20);


如您所见,我使用fsockopen而不是fopen来使我能够有20秒的超时请求,但是问题是我无法弄清楚我该如何传递需要使用fsockopen传递的请求字符串.从我从我的fsockopen代码中了解到,它只是尝试查看20秒内是否有来自$ url的回复.但它不发送任何字符串或请求字符串进行处理...
所以这就是我想要的...

1.)尝试从$ url发送请求字符串,并带有要在标头上传递的特定查询字符串
2.)等待$ url的回复,等待一定的时间,例如20秒
3.)处理正确的回复...

希望您能帮助我的问题专家...预先感谢

解决方案

url,


数据,

请求,


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);		
		
		//$fp = @fsockopen($url,443,$errno,$errstr,20);		
		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

解决方案

url,


data,


request,


这篇关于如何在PHP发送头中设置等待超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆