1

How would I write below command in curl php:

curl -XPOST https://apiv2.unificationengine.com/v2/message/send

–data “{ \"message\”: { \“receivers\”: [{\“name\”: \“TO_NAME \”,

\“address\”: \“TO_EMAILADDRESS\” ,

\“Connector\”: \“UNIQUE_CONNECTION_IDENTIFIER\”,

\“type\”: \“to\”}],\“sender\”: {\“address\”: \“EMAIL_ADDRESS\”},

\“subject\”:\“Hello\”,\“parts\”: [{\“id\”: \“1\”,

\“contentType\”: \“text/plain\”,

\“data\”:\“Hi welcome to UE\” ,

\“size\”: 100,\“type\”: \“body\”,\“sort\”:0}]}}“

-u USER_ACCESSKEY:USER_ACCESSSECRET -k

if this is the right way to execute and write curl in php:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://abcproject.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if ($server_output == "OK") { ... } else { ... }

Here is more sample code SO question.

But I am finding problem that where to mentions -u, --data options in php curl.

omega
  • 341
  • 1
  • 2
  • 17
Always_a_learner
  • 4,585
  • 13
  • 63
  • 112
  • 2
    Handling cURL calls in PHP is a gruesome task. Try using something like Guzzle - http://docs.guzzlephp.org/en/latest/- instead. It will save a lot of headaches... – motanelu Jan 05 '17 at 13:49
  • 1
    Do you want to post JSON data, right? – Jose Rojas Jan 05 '17 at 13:56
  • @JoseRojas I want to execute above command and want to know that how to do that. post parameters would be json I think. – Always_a_learner Jan 05 '17 at 13:57
  • That USER_ACCESSKEY:USER_ACCESSSECRET are username and password? – Jose Rojas Jan 05 '17 at 14:09
  • @JoseRojas those are user keys and user secret for my app that I have created for this api under my app. – Always_a_learner Jan 05 '17 at 14:10
  • @JoseRojas This is the url https://developer.unificationengine.com/ from where I have got this command. – Always_a_learner Jan 05 '17 at 14:12
  • @motanelu how I would convert this to guzzle command? I am confused that what -data and -u are? Are they post parameters or something else. – Always_a_learner Jan 05 '17 at 14:13
  • -data is the data that's being sent over the POST and -u are the credentials required for the authentication (you may not need those) – motanelu Jan 05 '17 at 14:17
  • @motanelu On developer.unificationengine.com they have mentioned to use user secret and access keys to use api so I cannot omit them. Thanks for telling that I have to use these parameters in post but now I want to know that where to use '-u' parameters? Do you have any idea? – Always_a_learner Jan 05 '17 at 14:21

2 Answers2

3

curl -u is equivalent to CURLOPT_USERPWD in php-curl.

You set it with curl_setopt, as the others.

--data is CURLOPT_POSTFIELDS, which you are already sending. But in your case you'd want to populate with the json you want to send.

If you are sending a JSON, you'd do well in setting the Content-type header (and content-length wouldn't be amiss either)

Be careful, in your example call there are some weird characters. But the JSON you posted is equivalent to:

$yourjson = <<<EOF
{
  "message": {
    "receivers": [
      {
        "name": "TO_NAME ",
        "address": "TO_EMAILADDRESS",
        "Connector": "UNIQUE_CONNECTION_IDENTIFIER",
        "type": "to"
      }
    ],
    "sender": {
      "address": "EMAIL_ADDRESS"
    },
    "subject": "Hello",
    "parts": [
      {
        "id": "1",
        "contentType": "text/plain",
        "data": "Hi welcome to UE",
        "size": 100,
        "type": "body",
        "sort": 0
      }
    ]
  }
}
EOF;

But usually you'd start with your data in array form and json_encode it.

So you'd start with something like:

$array = [
    'message' =>
        [
            'receivers' =>
                [
                    0 =>
                        [
                            'name'      => 'TO_NAME ',
                            'address'   => 'TO_EMAILADDRESS',
                            'Connector' => 'UNIQUE_CONNECTION_IDENTIFIER',
                            'type'      => 'to',
                        ],
                ],
            'sender'    =>
                [
                    'address' => 'EMAIL_ADDRESS',
                ],
            'subject'   => 'Hello',
            'parts'     =>
                [
                    0 =>
                        [
                            'id'          => '1',
                            'contentType' => 'text/plain',
                            'data'        => 'Hi welcome to UE',
                            'size'        => 100,
                            'type'        => 'body',
                            'sort'        => 0,
                        ],
                ],
        ],
];

... convert that using $yourjson = json_encode($array), and that's it.

E.g.:

// you already have your json inside of $yourjson,
// plus your username and password in their respective variables.

curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POSTFIELDS, $yourjson);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
              'Content-Type: application/json',                    
              'Content-Length: ' . strlen($yourjson)
               ]
);
yivi
  • 42,438
  • 18
  • 116
  • 138
1

to send data as JSON add header content-type and set as JSON and add the option CURLOPT_USERPWD, something like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://apiv2.unificationengine.com/v2/message/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS,
            “{ \"message\”: { \“receivers\”: [{\“name\”: \“TO_NAME \”, \“address\”: \“TO_EMAILADDRESS\” , \“Connector\”: \“UNIQUE_CONNECTION_IDENTIFIER\”, \“type\”: \“to\”}],\“sender\”: {\“address\”: \“EMAIL_ADDRESS\”},\“subject\”:\“Hello\”,\“parts\”: [{\“id\”: \“1\”,\“contentType\”: \“text/plain\”, \“data\”:\“Hi welcome to UE\” ,\“size\”: 100,\“type\”: \“body\”,\“sort\”:0}]}}“);

// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

$server_output = curl_exec ($ch);

curl_close ($ch);
Jose Rojas
  • 3,490
  • 3
  • 26
  • 40