How to send parameters in curl in php

2020-04-21 02:25发布

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.

标签: php curl
2条回答
不美不萌又怎样
2楼-- · 2020-04-21 03:09

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)
               ]
);
查看更多
我只想做你的唯一
3楼-- · 2020-04-21 03:13

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);
查看更多
登录 后发表回答