简体   繁体   中英

Error on cURL request using php-cURL while sending SMS

The request entity's media type 'multipart/form-data' is not supported for this resource.\\",\\"ExceptionMessage\\"unsure emoticon"No MediaTypeFormatter is available to read an object of type 'SmsQueue' from content with media type 'multipart/form-data'.\\",\\"ExceptionType\\"unsure emoticon"System.Net.Http.UnsupportedMediaTypeException\\",\\"StackTrace\\"unsure emoticon" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\\r\\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\\"}"

    // 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 => http_build_query($postArray) not working

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

Set Content-Type in header to application/x-www-form-urlencoded. Use like this :

        $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'
        ),
    ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM