Curl with false answer

Asked

Viewed 222 times

0

I’m integrating Zenvia to text a system.
I use the script below, second documentation.

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api-rest.zenvia.com/services/send-sms");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, "{
  \"sendSmsRequest\": {
    \"from\": \"Remetente\",
    \"to\": \"555199999999\",
    \"schedule\": \"2014-08-22T14:55:00\",
    \"msg\": \"Mensagem de teste\",
    \"callbackOption\": \"NONE\",
    \"id\": \"002\",
    \"aggregateId\": \"1111\",
    \"flashSms\": false
  }
}");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json",
  "Authorization: Basic YWRtaW46YWRtaW4=",
  "Accept: application/json"
));

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);

However, even filling the data with the real ones, the return is always false.

The same happens if I use the script below:

    $url = 'https://api-rest.zenvia.com/services/send-sms';
    $data = array(
        "from" => "Remetente",
        "to" => "555199999999",
        "schedule" => "2014-08-22T14:55:00",
        "msg" => "Mensagem de teste",
        "callbackOption" => "NONE",
        "id" => "002",
        "aggregateId" => "1111",
        "flashSms" => false
    );
    $options = array(
      'http' => array(
        'method'  => 'POST',
        'content' => json_encode( $data ),
        'header'=>  "Content-Type: application/json\r\n" .
                    "Authorization: Basic YWRtaW46YWRtaW4=\r\n" . 
                    "Accept: application/json\r\n"
        )
    );
    $context  = stream_context_create( $options );
    $result = file_get_contents( $url, false, $context );
    echo "<pre>";
    var_dump($result);

Am I doing something wrong? Or forgetting something ?
Some configuration on my server might be "blocking" something ?

  • Have you tried taking r n off the headlines.

1 answer

2


I tried to test it here, but it was denied access. But see if it works like this:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api-rest.zenvia.com/services/send-sms",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n\t\"from\": \"Remetente\",\n    \"to\": \"555199999999\",\n    \"schedule\": \"2014-08-22T14:55:00\",\n    \"msg\": \"Mensagem de teste\",\n    \"callbackOption\": \"NONE\",\n    \"id\": \"002\",\n    \"aggregateId\": \"1111\",\n    \"flashSms\": false\n}",
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Authorization: Basic YWRtaW46YWRtaW4=",
    "Content-Type: application/json",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
  • A hint is to first test in Postman and then generate Curl from it (https://learning.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets/)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.