如何在PHP中的curl中发送参数 [英] How to send parameters in curl in php
问题描述
我将如何在curl php中编写以下命令:
How would I write below command in curl php:
curl -XPOST https://apiv2.unificationengine.com/v2/message/send
–data
{\ message\:{\ receivers\:[{\ name\:\ TO_NAME \,
–data "{ \"message\": { \"receivers\": [{\"name\": \"TO_NAME \",
\地址\:\ TO_EMAILADDRESS\,
\"address\": \"TO_EMAILADDRESS\" ,
\连接器\:\ UNIQUE_CONNECTION_IDENTIFIER\,
\"Connector\": \"UNIQUE_CONNECTION_IDENTIFIER\",
\ type\:\ to\}],\ sender\:{\地址\:\ EMAIL_ADDRESS\},
\"type\": \"to\"}],\"sender\": {\"address\": \"EMAIL_ADDRESS\"},
\ subject\:\ Hello\,\ parts\ \:[{\ id\:\ 1\,
\"subject\":\"Hello\",\"parts\": [{\"id\": \"1\",
\ contentType\:\ text / plain \,
\"contentType\": \"text/plain\",
\数据\:\欢迎来到UE\,
\"data\":\"Hi welcome to UE\" ,
\ size\:100,\ type\:\ body\,\ sort\:0}]}}}
\"size\": 100,\"type\": \"body\",\"sort\":0}]}}"
-u USER_ACCESSKEY:USER_ACCESSSECRET -k
-u USER_ACCESSKEY:USER_ACCESSSECRET -k
如果这是执行和写入的正确方法在php中卷曲:
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.
但是我发现在PHP curl中在哪里提到-u,--data选项的问题。
But I am finding problem that where to mentions -u, --data options in php curl.
推荐答案
卷曲 -u
等同于 CURLOPT_USERPWD
在php-curl中。
curl -u
is equivalent to CURLOPT_USERPWD
in php-curl.
您使用 curl_setopt 以及其他。
- -data
是 CURLOPT_POSTFIELDS
,您已经在发送该数据了。但是,在您的情况下,您想使用要发送的JSON填充。
--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.
如果要发送JSON,则最好设置 Content-type
标头(也不会出现content-length)
If you are sending a JSON, you'd do well in setting the Content-type
header (and content-length wouldn't be amiss either)
请小心,在您的示例调用中是一些奇怪的人物。但是您发布的JSON等效于:
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;
但是通常您会以数组形式和 json_encode
。
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,
],
],
],
];
...使用 $ yourjson = json_encode($ array)<进行转换/ code>就是这样。
... convert that using $yourjson = json_encode($array)
, and that's it.
例如:
// 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)
]
);
这篇关于如何在PHP中的curl中发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!