[英]PHP Loop with Conditional Statement for CURL Execution
I am using 2 functions to execute a CURL. 我正在使用2个函数来执行CURL。 The first function retrieves and store a token while the other executes the order with the token stored.
第一个函数检索并存储令牌,而另一个函数使用存储的令牌执行订单。
I am using file_put_contents and file_get_contents to store and retrieve the token. 我正在使用file_put_contents和file_get_contents来存储和检索令牌。
Function A: 功能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;
}
Function B: 功能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);
As you can see, I will need to re-run Function A before initiating Function B if the token has expired. 如您所见,如果令牌已过期,则在启动功能B之前,我将需要重新运行功能A。 I have used the status 401 that I store as a variable $httpcode.
我已经使用状态401存储为变量$ httpcode。
I am unsure how to re-run Function B upon getting the new/refreshed token. 我不确定如何在获得新的/刷新的令牌后重新运行功能B。 Which loop and condition should I be implementing to achieve this?
我应该实现哪个循环和条件来实现这一目标?
Can anyone point this out? 有人可以指出吗? Any assistance is greatly appreciated.
非常感谢您的协助。
Thank you. 谢谢。
A very basic concept at re-try framework, provided you can keep a cache of the last time you fetched the token. 重试框架中的一个非常基本的概念,条件是您可以保留上一次获取令牌的缓存。
Refactor with the status you need to determine success/failure 重构您确定成功/失败所需的状态
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--;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.