用于CURL执行的条件语句的PHP循环

时间:2017-12-21 03:54:13

标签: php if-statement curl while-loop do-while

我使用2个函数来执行CURL。第一个函数检索并存储一个令牌,而另一个函数执行订单并存储令牌。

我正在使用file_put_contents和file_get_contents来存储和检索令牌。

功能A:

function functionA() {
$ch = curl_init();

$url_token = $this->config['TOKEN'];
$id = $this->config['ID'];
$secret = $this->config['SECRET'];

curl_setopt($ch, CURLOPT_URL, $url_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, "{
  \"client_id\": \"$id\",
  \"client_secret\": \"$secret\",
  \"grant_type\": \"client_credentials\"
}");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json",
  "Accept: application/json"
));

$response = curl_exec($ch);
curl_close($ch);

$json = json_decode($response, true);
$file = 'key.txt';
$token = $json['access_token'];

file_put_contents('./modules/custommodule/key.txt', $token, LOCK_EX);

//return $token;
}

功能B:

$file = './modules/custommodule/key.txt';
$retrieved_token = file_get_contents($file);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_order);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, "{
  \"service_type\": \"xxx\",
  \"service_level\": \"xxx\",
  \"requested_tracking_number\": \"xxx\",
  \"reference\": {
    \"merchant_order_number\": \"xxx\"
  },
  \"from\": {
    \"name\": \"xxx\",
    \"phone_number\": \"xxx\",
    \"email\": \"xxx\",
    \"address\": {
      \"address1\": \"xxx\",
      \"address2\": \"xxx\",
      \"area\": \"xxx\",
      \"city\": \"xxx\",
      \"state\": \"xxx\",
      \"country\": \"xxx\",
      \"postcode\": \"xxx\"
    }
  },
  \"to\": {
    \"name\": \"xxx\",
    \"phone_number\": \"xxx\",
    \"email\": \"xxx\",
    \"address\": {
      \"address1\": \"xxx\",
      \"address2\": \"xxx\",
      \"area\": \"xxx\",
      \"city\": \"xxx\",
      \"state\": \"xxx\",
      \"country\": \"xxx\",
      \"postcode\": \"xxx\"
    }
  },
  \"parcel_job\": {
    \"pickup_instruction\": \"xxx\",
    \"delivery_instruction\": \"xxx\",
    \"delivery_start_date\": \"xxx\",
    \"delivery_timeslot\": {
      \"start_time\": \"xxx\",
      \"end_time\": \"xxx\",
      \"timezone\": \"xxx\"
    },
    \"dimensions\": {
      \"size\": \"xxx\"
    }
  }
}");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json",
  "Accept: application/json",
  "Authorization: Bearer $retrieved_token"
));

$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($httpcode === 401) {
    functionA();
    }
}

curl_close($ch);

file_put_contents('./modules/custommodule/response.txt', $response, LOCK_EX);
file_put_contents('./modules/custommodule/results.txt', $httpcode, LOCK_EX);

如您所见,如果令牌已过期,我将需要在启动功能B之前重新运行功能A.我使用了我存储的状态401作为变量$ httpcode。

我不确定如何在获取新的/刷新的令牌时重新运行功能B.我应该实现哪种循环和条件来实现这一目标?

任何人都可以指出这一点吗?非常感谢任何帮助。

谢谢。

1 个答案:

答案 0 :(得分:0)

重试框架的一个非常基本的概念,前提是您可以保留上次获取令牌的缓存。

重构具有确定成功/失败所需的状态

function CurlRequestUsingTokenWithRetry( $curRequest, $previousToken )
{

   $token = $previousToken;

   $retries = 10;

   while( $retries > 0 )
   {

      $result = PerformTheCurlRequest( $curRequest, $token );

      if( $resultStatusCode == 401 )
      {
        $token = GetNewToken();
        if( !$token )
        {
          throw new Exception('getting new token failed');
        }
      }
      elseif( $resultStatusCode == 403 )
      {
        throw new Exception('oh no you are blocked');
      }
      else
      {
        return $reuslt;
      }

      $retries--;

   }

}