0

soy muy nuevo usando cURL en PHP, quisiera usar esta API para enviar SMS pero cuando hago todas las pruebas me sale el siguiente error:

HTTP/1.1 400 Bad Request Content-Length: 0 X-Application-Context: application:production:8080

He revisado mi código y no entiendo que esta pasando realmente:

$data=array('from' => '506712xxxx', 'to' => '50671xxxx', 'body' => 'Hola este es un mensaje de prueba' );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://sms.api.sinch.com/xms/v1/xxxxx/batches");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: Bearer xxxxxx",
    "Content-Type: application/json",
  ));
$res = curl_exec($ch);
print_r($res);
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch);

He revisado la documentación oficial y no se que estoy haciendo mal, la documentación oficial: https://www.sinch.com/docs/sms/http-rest.html

4
  • no se ve mal el código, has probado con el comando curl como te muestra la documentación ? el 25 feb. 2019 a las 17:29
  • No se como lo haria, gracias Mauricio por responder el 25 feb. 2019 a las 19:48
  • Hay algunos REST Api Client .. algunos se ejecutan desde el Chrome y quizas puedan servirte . El error 400 es, segpun la documentación, un problema en los datos que envías si mal no entiendo .. Quizas te este faltando algun Token de seguridad o similar.. el 25 feb. 2019 a las 22:26
  • Si el token esta en la línea "Authorization: Bearer xxxxxx", que no muestro por seguridad. y en el link esta el usuario, gracias por el comentario el 25 feb. 2019 a las 22:34

1 respuesta 1

0

el código final:

<?
$data=array('from' => 'Oso', 'to' => array('57xx'), 'body' => 'Hola este es un mensaje de prueba' );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://sms.api.sinch.com/xms/v1/xxxx/batches");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,  json_encode($data));
curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: Bearer xxxxx",
    "Content-Type: application/json",
  ));
$res = curl_exec($ch);
print_r($res);
//var_dump(curl_getinfo($ch));
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch);
?>

-agregue un array() adicional en "to" -convertí a Json con la función json_encode -Posdata: Las "xxx" eran la llave y el numero de cuenta

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.