将值应用到PHP上的cURL中
我有一个关于卷曲的问题。 以下是将由用户输入更改的值将值应用到PHP上的cURL中,php,curl,Php,Curl,我有一个关于卷曲的问题。 以下是将由用户输入更改的值 $uid = "xxxxx" //Get from system $token = "xxxxx" //Get from system $title = "Testing Message"; //user input $body = "Message"; //user input $url_on_click = "https://www.xxxxx.co
$uid = "xxxxx" //Get from system
$token = "xxxxx" //Get from system
$title = "Testing Message"; //user input
$body = "Message"; //user input
$url_on_click = "https://www.xxxxx.com"; //user input
我想将该值应用到函数中,但出现以下错误:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://bn.xxxxxx.com/wpn/wpn_send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"uid" :' . $record_uid . ',
"payload": ' . $payload . '
}',
CURLOPT_HTTPHEADER => array(
'token: ' . $record_token . ',
Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
如果我将所有值都更改为字符串。这是工作。代码如下所示:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://bn.xxxxxx.com/wpn/wpn_send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{"uid" :"xxxxxx",
"payload": {
"title": "Testing Message",
"body": "1234",
"icon":"https://xxxxx.net/xxxxx.png",
"urlOnClick":"xxxxx.com"
}}',
CURLOPT_HTTPHEADER => array(
'token: xxxxxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
我能知道我做错了哪一部分吗?那个问题真让我恼火。
谢谢=]试试这个,post字段取决于您的
CURLOPT_HTTPHEADER
,根据您的头,您需要在JSON
中转换它,否则,数组
类型将起作用
$postFields = array(
'uid' => "xxxx",
'token' => "xxxx",
'title' => "xxxx",
'body' => "xxxx",
'url_on_click' => "xxxx"
);
//curl init lines
//-------
//-------
CURLOPT_POSTFIELDS => json_encode($postFields),
这是因为JSON无效(缺少引号)。使用而不是自己创建它。
但它出现错误
~错误是什么?正如@Syscall已经建议的那样~这可能是一个JSON编码错误,但是如果您包含错误消息,那就太好了@Syscall Ya,json_encode()不正确,因为它返回带“\”的字符串。我使用json_encode($array,json_UNESCAPED_SLASHES)将正确的数据发送到API。谢谢你