使用PHP调用Clickatell API发短信遇HTTP 405错误求助
Fixing "HTTP 405 Method Not Allowed" Error with Clickatell SMS API in PHP
Let's break down why you're hitting this error and fix it step by step:
1. What's Causing the 405 Error?
You've got two main issues here:
- Wrong HTTP Method: You're sending a
POSTrequest, but Clickatell's traditional HTTP send endpoint expects aGETrequest. - Malformed URL: Your URL has critical formatting mistakes:
- No
?separator between the endpoint path (send) and the first query parameter (apiKey), so it's readingsendapiKey=as part of the path instead of a parameter. - Using HTML-encoded
&instead of plain&to separate query parameters — curl can't parse these correctly.
- No
2. Corrected Code
Here's the fixed version with explanations:
// Fix the URL structure: add ? after send, replace & with & $url = 'https://platform.clickatell.com/messages/http/send?apiKey=ordnzDdGdRhqdnjdf3ZmzYA==&to=904234234&content=your+password+is+BhSTV'; $ch = curl_init($url); // Keep this to capture the response as a string instead of outputting it directly curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Disable SSL verification (note: for production, enable this with valid certificates) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // REMOVE the POST option — this API requires GET requests // curl_setopt($ch, CURLOPT_POST, 1); // Execute the request $response = curl_exec($ch); // Add error checking to catch curl-specific issues if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); } else { echo $response; } // Clean up the curl handle curl_close($ch);
3. Key Fixes Explained
- URL Correction: Added
?to separate the endpoint from parameters, and swapped&for plain&so curl can read each parameter properly. - HTTP Method Match: Removed the
CURLOPT_POSTsetting to use curl's defaultGETmethod, which aligns with Clickatell's API requirements. - Error Handling: Added
curl_errno()checks to catch any unexpected curl issues that might pop up later.
Bonus Production Tip
For live environments, turn CURLOPT_SSL_VERIFYPEER back to true — disabling it skips SSL certificate validation, which is a security risk. If your server doesn't have a default CA bundle, you can specify one using CURLOPT_CAINFO.
内容的提问来源于stack exchange,提问作者Abobaker EngIt
相关产品推荐
相关产品推荐

