You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  • mslist Structure: Changed from a flat array of phone numbers to an array containing an associative array with phone1 and phone2 keys. This exactly matches the JSON structure you specified in your target request.
  • Parameter Name Correction: Fixed custretryatmpt to custretry to align with your original payload ("custretry": 1).
  • Query String Improvement: Updated http_build_query to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 21:47:39