message_media_builder.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. // Function Explanation
  3. // This function is used for providers that do not send a true array for message media.
  4. // It will take a $searchArray, which is typically a $_GET or $_POST array,
  5. // and get all of the array keys that start with one of the entries in the $keys array
  6. // and append them to a new array.
  7. // Keys added to the new array will be suffixed with $index
  8. // This is used for inbound message media array
  9. // Example usage:
  10. // $keys = ['message_media_url', 'message_media_type'];
  11. // $arr = message_media_builder($_POST, $keys);
  12. // Contents of $arr
  13. // [
  14. // 'message_media_url0' => 'https://sms.provider.example.com/img1.jpg',
  15. // 'message_media_type0' => 'image/jpeg',
  16. // 'message_media_url1' => 'https://sms.provider.example.com/img1.jpg',
  17. // 'message_media_type1' => 'image/jpeg'
  18. // ]
  19. function message_media_builder($searchArray, $keys){
  20. $mediaArray = [];
  21. $index = 0;
  22. while($index >= 0){
  23. $insert = true;
  24. foreach ($keys as $key){
  25. $param = $key.strval($index);
  26. if(!isset($searchArray[$param])){
  27. $insert = false;
  28. break;
  29. }
  30. $tmp[$key] = $searchArray[$param];
  31. }
  32. if($insert == false){ break; }
  33. $mediaArray[] = $tmp;
  34. $index++;
  35. }
  36. return $mediaArray;
  37. }
  38. ?>