fork download
  1. <?php
  2. // Initialize cURL session to send a GET request
  3. $url = "https://c...content-available-to-author-only...e.com/api/challenges/json/age-counting";
  4.  
  5. // Create a cURL handle
  6. $ch = curl_init();
  7.  
  8. // Set the cURL options
  9. curl_setopt($ch, CURLOPT_URL, $url);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
  11. curl_setopt($ch, CURLOPT_HTTPGET, true); // Use GET method
  12.  
  13. // Execute the cURL session and store the response
  14. $response = curl_exec($ch);
  15.  
  16. // Check if the request was successful
  17. if (curl_errno($ch)) {
  18. echo 'Curl error: ' . curl_error($ch);
  19. } else {
  20. // Close the cURL session
  21. curl_close($ch);
  22.  
  23. // Decode the JSON response into an associative array
  24. $data = json_decode($response, true);
  25.  
  26. // Extract the 'data' value from the response
  27. $dataString = $data['data'];
  28.  
  29. // Split the data string by commas to separate key-value pairs
  30. $pairs = explode(', ', $dataString);
  31.  
  32. // Initialize count for items with age >= 50
  33. $count = 0;
  34.  
  35. // Loop through each pair and extract the age
  36. foreach ($pairs as $pair) {
  37. // Check if the pair contains an "age" key-value pair
  38. if (strpos($pair, 'age=') !== false) {
  39. // Extract the age value using regular expression
  40. preg_match('/age=(\d+)/', $pair, $matches);
  41.  
  42. // If we found a valid match for the age
  43. if (isset($matches[1])) {
  44. $age = (int)$matches[1];
  45.  
  46. // Check if the age is greater than or equal to 50
  47. if ($age >= 50) {
  48. $count++;
  49. }
  50. }
  51. }
  52. }
  53.  
  54. // Output the final count
  55. echo "Number of items with age >= 50: $count\n";
  56. }
  57. ?>
  58.  
Success #stdin #stdout 0.04s 26068KB
stdin
Standard input is empty
stdout
Curl error: Could not resolve host: coderbyte.com