【问题标题】:Error on cURL request using php-cURL while sending SMS发送短信时使用 php-cURL 的 cURL 请求出错
【发布时间】:2020-08-31 21:47:30
【问题描述】:

请求实体的媒体类型“multipart/form-data”不是 支持此资源。\",\"ExceptionMessage\"不确定表情"否 MediaTypeFormatter 可用于读取“SmsQueue”类型的对象 来自具有媒体类型的内容 'multipart/form-data'.\",\"ExceptionType\"不确定 表情"System.Net.Http.UnsupportedMediaTypeException\",\"StackTrace\"不确定 图释”在 System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent 内容,类型类型,IEnumerable 1 格式化程序,IFormatterLogger formatterLogger, CancellationToken cancelToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage 请求,类型类型,IEnumerable`1 格式化程序,IFormatterLogger formatterLogger, CancellationToken cancelToken)\"}"

    // Prepare you post parameters
    $postArray = array(
        'APIKey' => AUTH_KEY,
        'number' => $mobile,
        'text' => $message,
        'senderid' => SENDER_ID,
        'channel' => $channel,
        'DCS' => $DCS,
        'flashsms' => $flashsms,
        'route' => $route
    );

    // Init the resource
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $postArray
    ));

    // Ignore SSL certificate verification
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    // Get response
    $curlOutput = curl_exec($ch);

    // Print error if any
    if (curl_errno($ch)) {
        echo 'error:' . curl_error($ch);
    }

    curl_close($ch);

【问题讨论】:

  • 代替CURLOPT_POSTFIELDS => $postArray 试试CURLOPT_POSTFIELDS => http_build_query($postArray)。我在 Ecobee API 上发布了类似的错误,这为我解决了问题。
  • 谢谢你的作品。

标签: php curl php-curl


【解决方案1】:

CURLOPT_POSTFIELDS => http_build_query($postArray) 不工作

curl_setopt($cURLConnection, CURLOPT_POSTFIELDS => http_build_query($postRequest1));

【讨论】:

  • 您能否通过以下方式改进您的答案:a)发布语法正确的代码,b)使用编辑器的降价选项格式化您的代码,c)详细说明建议的解决方案。
【解决方案2】:

将标头中的 Content-Type 设置为 application/x-www-form-urlencoded。 像这样使用:

        $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "http://url.com",
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => http_build_query(['message' => test]),
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/x-www-form-urlencoded'
        ),
    ));

【讨论】:

    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 2019-09-29
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    相关资源
    最近更新 更多