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.

9
  • 2
    Handling cURL calls in PHP is a gruesome task. Try using something like Guzzle - docs.guzzlephp.org/en/latest/- instead. It will save a lot of headaches... Commented Jan 5, 2017 at 13:49
  • 1
    Do you want to post JSON data, right? Commented Jan 5, 2017 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. Commented Jan 5, 2017 at 13:57
  • That USER_ACCESSKEY:USER_ACCESSSECRET are username and password? Commented Jan 5, 2017 at 14:09
  • @JoseRojas those are user keys and user secret for my app that I have created for this api under my app. Commented Jan 5, 2017 at 14:10

2 Answers 2

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)
               ]
);
Sign up to request clarification or add additional context in comments.

5 Comments

Let me try this. Thanks!
I am getting invalid character &#39;-&#39; in numeric literal in response.
That's an apostroph, or single quote. There is something wrong with your data.
YES!! You saved my day big time! Though I have to check few more urls. And invalid character &#39;-&#39; in numeric literal was there because I was sending array instead of json in post params.
Yes It did. Thank you. For current scenario it did. I was really confuse because I never used Curl before but my bad that I didn't have sufficient time to do RnD on this. I hope this question help someone in some way. Cheers!!
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);

3 Comments

Thanks Jose! Let me try both the solutions.
Thank you So much @Jose. You all saved my day!
Yes @Jose it worked. But unfortunately I can mark only one answer. I am really thankful for your great answer. For those who will look for right answer for anything like I have posted. Your answer is also 100% correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.