在 php 中不接受第一个响应时如何继续请求?

how to continue requesting when first response is not accepted in php?

我是使用 Guzzle 包的新手 我想通过网络发送数据 api 当响应状态为 OK 或 NOT还睡了 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

那么如何根据响应状态重新发送呢? 谢谢

如果状态不是 'ok' 如果您想再次发送,则:

if($contents!=='ok'){
    $response = $client->Request("POST", $url, [
        'handler'  => $stack,
        'headers'=>$headers,
        'form_params'=>$body
    ]);
    $contents = (string) $response->getBody();
}

如果状态是指 http 状态,那么您可以这样验证:

$status = $response->getStatusCode();
if($status!==200){
 //your request again
}

或者我理解错了你的问题。那样的话请详细说明。

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