PHP CURL API请求参数配置求助:mslist中phone参数错误
Fixing the
mslist Parameter in Your PHP cURL Request Hey there! I see the issue with how you're adding phone1 and phone2 to the mslist parameter. Your current code puts the encoded phone numbers directly into an array, but the target API expects mslist to be an array of associative arrays (PHP's equivalent of JSON objects) where each entry has explicit phone1 and phone2 keys.
Corrected Code (GET Request)
$phone1 = "XXXXXX"; $phone2 = "XXXXXXX"; // CURL setup $url = 'http://api-ip-XXXX'; $param = array( 'source' => '0', 'custom' => true, 'credit' => '2', 'filetype' => '2', 'ustate' => 'jkghlyt', 'service' => '1234', 'templateid' => '632', 'retryatmpt' => '1', 'retryduration' => '1', 'custretry' => '1', // Fixed parameter name to match your target payload 'refno' => true, 'mslist' => array( array( 'phone1' => urlencode($phone1), 'phone2' => urlencode($phone2) ) ) ); // Build query string correctly (no numeric prefixes, clean separator) $url .= "?" . http_build_query($param, '', '&'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $result = curl_exec($ch); // Optional: Check for cURL errors if(curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } curl_close($ch);
Key Fixes & Explanations
mslistStructure: Changed from a flat array of phone numbers to an array containing an associative array withphone1andphone2keys. This exactly matches the JSON structure you specified in your target request.- Parameter Name Correction: Fixed
custretryatmpttocustretryto align with your original payload ("custretry": 1). - Query String Improvement: Updated
http_build_queryto avoid unnecessary numeric prefixes and use a clean&separator for better compatibility.
Bonus: POST Request with JSON Body (Recommended for Complex Payloads)
Most APIs prefer JSON payloads for complex data like this instead of GET query strings. Here's how to adjust your code to send the exact JSON structure you provided:
$phone1 = "XXXXXX"; $phone2 = "XXXXXXX"; $url = 'http://api-ip-XXXX'; $payload = array( 'source' => '0', 'custom' => true, 'credit' => '2', 'filetype' => '2', 'ustate' => 'jkghlyt', 'service' => '1234', 'templateid' => '632', 'retryatmpt' => 1, 'retryduration' => 1, 'custretry' => 1, 'refno' => true, 'mslist' => array( array( 'phone1' => $phone1, 'phone2' => $phone2 ) ) ); $jsonPayload = json_encode($payload); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Set headers for JSON content curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($jsonPayload) )); // Send POST request with JSON body curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload); $result = curl_exec($ch); // Check for errors if(curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } curl_close($ch);
内容的提问来源于stack exchange,提问作者Pamela
相关产品推荐
相关产品推荐

