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.
curl
-u
is equivalent toCURLOPT_USERPWD
in php-curl.You set it with curl_setopt, as the others.
--data
isCURLOPT_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:
But usually you'd start with your data in array form and
json_encode
it.So you'd start with something like:
... convert that using
$yourjson = json_encode($array)
, and that's it.E.g.:
to send data as JSON add header
content-type
and set as JSON and add the optionCURLOPT_USERPWD
, something like this: