我是使用Guzzle包的新手我希望通过web api发送数据当响应带有状态OK或NOT我做一些动作否则状态相等等待我再次请求5秒或状态相等尚未睡眠30秒。 这是我的代码
$client = new Client();
$headers= [
'Accept' => 'application/x-www-form-urlencoded',
'Content-Type' => 'application/x-www-form-urlencoded',
];
$body = [
'phone2'=>'723457481',
'amount'=>'200'
];
$url = "http://192.168.31.51:8080/requesttrafic/";
$response = $client->Request("POST", $url, [
'handler' => $stack,
'headers'=>$headers,
'form_params'=>$body
]);
$contents = (string) $response->getBody();
// this $contents can be status 'ok','not' anything
那么如何根据响应状态再次发送? 感谢
答案 0 :(得分:0)
如果您想要再次发送状态,如果状态不正常'然后:
$status = $response->getStatusCode();
if($status!==200){
//your request again
}
如果状态是您的http状态,那么您可以像这样验证:
html {background: #eee;}
* {padding: 0; margin: 0;}
html, body {height: 100%;text-align: center;}
.container{display: inline-block; vertical-align: middle; margin-top: 10vh; background: rgba(200,200,200,0.5);
display:flex;}
@media screen and (orientation:landscape) {.container {width: 60vw; height: 80vh;}}
@media screen and (orientation:portrait) {.container {width: 80vw; height: 80vh;}}
.div1 {display: block; background: rgba(80,0,0,0.5); width:100px; height:100px;float:left;}
.div2 {display: block; background: rgba(0,80,0,0.5); width:200px; height:100px;float:left;}
.div3 {display: block; background: rgba(0,0,80,0.5); width:100px; height:200px;float:left;}
.div4 {display: block; background: rgba(80,0,0,0.5); width:100px; height:100px;float:left;}
.div5 {display: block; background: rgba(0,80,0,0.5); width:200px; height:100px;float:left;}
.div6 {display: block; background: rgba(0,0,80,0.5); width:100px; height:200px;float:left;}
.div7 {display: block; background: rgba(80,0,0,0.5); width:100px; height:100px;float:left;}
或者我理解你的问题是对的。在那种情况下请详细说明。
答案 1 :(得分:0)
$response = $client->Request("POST", $url, [
'handler' => $stack,
'headers'=>$headers,
'form_params'=>$body
]);
$contents = (string) $response->getBody();
if($contents!=='ok'){
$response = $client->Request("POST", $url, [
'handler' => $stack,
'headers'=>$headers,
'form_params'=>$body
]);
$contents = (string) $response->getBody();
if($contents!=='ok'){
$response = $client->Request("POST", $url, [
'handler' => $stack,
'headers'=>$headers,
'form_params'=>$body
]);
$contents = (string) $response->getBody();
}else{
exit;
}
}