如何在php中发送参数curl

时间:2017-01-05 13:43:03

标签: php curl

如何在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 \”:\“欢迎来到UE \”,

     

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

     

-u USER_ACCESSKEY:USER_ACCESSSECRET -k

如果这是在php中执行和编写curl的正确方法:

$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 { ... }

以下是更多示例代码SO question

但我发现问题是在php curl中提到-u, - data选项的位置。

2 个答案:

答案 0 :(得分:3)

curl -u相当于php-curl中的CURLOPT_USERPWD

您可以像其他人一样使用curl_setopt进行设置。

--dataCURLOPT_POSTFIELDS,您已经发送了。但在你的情况下,你想要填充你想要发送的json。

如果您要发送JSON,您可以设置Content-type标题(并且内容长度也不会出错)

小心,在你的示例调用中有一些奇怪的字符。但是您发布的JSON等同于:

$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;

但通常你会以数组形式和json_encode来开始使用数据。

所以你要从以下内容开始:

$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,
                        ],
                ],
        ],
];

...使用$yourjson = json_encode($array)转换它,就是这样。

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)
               ]
);

答案 1 :(得分:1)

以JSON方式发送数据添加标题content-type并设置为JSON并添加选项CURLOPT_USERPWD,如下所示:

$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);