title: PHP Loop with Conditional Statement for CURL Execution
body: <p>I am using 2 functions to execute a CURL. The first function retrieves and store a token while the other executes the order with the token stored.</p>
  
  <p>I am using file_put_contents and file_get_contents to store and retrieve the token.</p>
  
  <p>Function A:</p>
  
  <pre><code>function functionA() {
  $ch = curl_init();
  
  $url_token = $this-&gt;config['TOKEN'];
  $id = $this-&gt;config['ID'];
  $secret = $this-&gt;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;
  }
  </code></pre>
  
  <p>Function B: </p>
  
  <pre><code>$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);
  </code></pre>
  
  <p>As you can see, I will need to re-run Function A before initiating Function B if the token has expired. I have used the status 401 that I store as a variable $httpcode.</p>
  
  <p>I am unsure how to re-run Function B upon getting the new/refreshed token. Which loop and condition should I be implementing to achieve this?</p>
  
  <p>Can anyone point this out? Any assistance is greatly appreciated.</p>
  
  <p>Thank you.</p>