使用Africa Talking API发送短信时Curl请求返回False求助
Hey there, let's get to the bottom of why your PHP cURL request is returning false — that usually means the request didn't even make it to the API server, so first we need to diagnose cURL-specific errors, then fix the code issues.
Step 1: Add cURL Error Debugging
When curl_exec() returns false, the first move is to check what exactly went wrong with the cURL call. Add these lines right after curl_exec($ch) to get detailed error info:
$server_output = curl_exec($ch); if ($server_output === false) { echo 'cURL Error: ' . curl_error($ch); echo 'Error Code: ' . curl_errno($ch); } curl_close($ch);
This will tell you if the issue is related to SSL connections, timeouts, invalid headers, or something else entirely.
Step 2: Fix Key Issues in Your PHP Code
Comparing your code to the official cURL example, here are the critical adjustments needed:
1. Missing Accept Header
The official example includes Accept: application/json, but your code doesn't include this header. The API likely expects this to return a valid JSON response, so add it to your CURLOPT_HTTPHEADER array.
2. Properly Encode POST Fields
While PHP can auto-encode an array for application/x-www-form-urlencoded, it's more reliable to explicitly use http_build_query() to match the exact format of the official example. Replace your CURLOPT_POSTFIELDS line with:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($messages));
3. Verify Credentials & Parameters
- Make sure
usernameis set to your actual Africa Talking username (not just'sandbox'— that's only valid if your account is configured for sandbox testing). - Double-check the
toparameter format: it should be comma-separated numbers with full country codes (e.g.,+2661234567,+2667654321— no spaces, no missing+symbol). - Confirm your
apiKeyis correct (sandbox API keys are different from production keys if you're using the sandbox endpoint).
Modified Working Code Example
Here's the updated code with all fixes and error handling built in:
$messages = array( 'username' => 'your_actual_username', // Replace with your real username 'to' => '+266XXXXXXX,+266XXXXXX', // Full country codes, comma-separated 'message' => 'Hello from Africa Talking!', 'from' => 'your_short_code' // Replace with your short code or sender ID ); $url = "https://api.sandbox.africastalking.com/version1/messaging"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Add the missing Accept header and correct header formatting curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json', 'Content-Type: application/x-www-form-urlencoded', 'apiKey: your_real_api_key' // Replace with your actual API key )); // Explicitly encode POST fields to match the required form-urlencoded format curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($messages)); $server_output = curl_exec($ch); // Debug cURL errors if the request fails if ($server_output === false) { echo 'cURL Error: ' . curl_error($ch); echo '<br>Error Code: ' . curl_errno($ch); } else { // Decode and inspect the API's actual response $response = json_decode($server_output, true); var_dump($response); } curl_close($ch);
Additional Checks
- If you're testing in production, switch to the non-sandbox URL:
https://api.africastalking.com/version1/messaging(and use your production API key/username). - If you run into SSL errors, you might need to add
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);as a temporary fix — note this is insecure for production, so it's better to resolve any SSL certificate issues on your server long-term.
内容的提问来源于stack exchange,提问作者Stack

