PHP数组赋值及sendMessage函数相关技术咨询
sendMessage Function Refinement Guide Hey there! Let's walk through how to properly populate dynamic values into your PHP array and spruce up your sendMessage function to make it reusable and robust.
1. Dynamic Value Population (Replace Hardcoded Placeholders)
Right now, your function has fixed values like 'value1', 'value2', and placeholder strings like [id] in the URL. To make this practical, we’ll turn these into function parameters so you can pass in dynamic data every time you call the function.
Here’s how to adjust it:
- Add parameters to the function for dynamic content, heading, item ID, and user image URL.
- Use
str_replace()to swap out placeholders in your URL and image path.
Example of the updated function with dynamic inputs:
function sendMessage($contentEn, $headingEn, $itemId, $userImageUrl) { // Populate dynamic content/headings $content = array("en" => $contentEn); $headings = array("en" => $headingEn); // Replace placeholders in URLs $targetUrl = str_replace("[id]", $itemId, 'http://www.999999.in/view.php?id=[id]'); $chromeImageUrl = str_replace("[user_image]", $userImageUrl, 'http://www.99999.in/admin/user_images/[user_image]'); $hashes_array = array(); $fields = array( 'app_id' => "31fe2347-5f39-4d9f-2222-79bf542f00f9", 'included_segments' => array('All'), 'data' => array("foo" => "bar"), 'contents' => $content, 'headings' => $headings, 'url' => $targetUrl, 'chrome_web_image' => $chromeImageUrl ); // Optional: Add code to send the request (e.g., to a notification API) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications"); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic YOUR_REST_API_KEY')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $response = curl_exec($ch); curl_close($ch); return $response; }
2. Best Practices for Array Assignment
- Avoid Hardcoded Configs: Move static values like
app_idto a config file or environment variable instead of embedding them in the function. This makes updates way easier later.// Example config approach define('ONESIGNAL_APP_ID', '31fe2347-5f39-4d9f-2222-79bf542f00f9'); // Then use ONESIGNAL_APP_ID in the $fields array - Default Values for Optional Params: If some fields are optional, set default values in the function parameters so you don’t have to pass every value every time.
function sendMessage($contentEn, $headingEn, $itemId = null, $userImageUrl = null) { // Handle optional fields - e.g., only add 'url' if $itemId is set if ($itemId) { $fields['url'] = str_replace("[id]", $itemId, 'http://www.999999.in/view.php?id=[id]'); } } - Validate Inputs: Add checks to ensure required parameters aren’t empty before building the array. This prevents invalid API requests.
if (empty($contentEn) || empty($headingEn)) { throw new Exception("Content and heading are required!"); }
3. Final Notes
Your original $hashes_array is declared but not used—make sure to add it to the $fields array if it’s required by your notification service (e.g., action buttons for OneSignal).
To use the function, just call it with your dynamic values:
$response = sendMessage( "Your notification content here", "Notification Heading", "123", // Item ID "user123_profile.jpg" // User image filename );
内容的提问来源于stack exchange,提问作者user2226832

