我正在尝试通过chatfuel Broadcasting API
发送消息。我已经在邮递员上尝试过了,而且行得通。但是使用php无效,并给我false
的结果。
我的邮递员要求:
邮递员头:
我成功收到消息:
{
"result": "ok",
"success": true
}
我正在尝试通过代码来实现。所以我尝试了:
// Create map with request parameters
$params = array ('chatfuel_token' => 'protected', 'chatfuel_block_name' => 'protected');
// Build Http query using params
$query = http_build_query ($params);
$user_id = "userid";
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: keep-alive \r\n".
"Content-Length: 30 \r\n",
'Content-Type' => 'application/json',
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'https://api.chatfuel.com/bots/protected/users/'. $user_id .'/send', // page url
false,
$context);
但是我得到了
{
"result": "0",
"success": false
}
答案 0 :(得分:0)
以下PHP代码段与您在Postman上所做的相对应。
<?php
$request = new HttpRequest();
$request->setUrl('https://api.test.com');
$request->setMethod(HTTP_METH_POST);
$request->setQueryData(array(
'chatfuel_token' => 'tokenid',
'chatfueld_block_name' => 'blok1'
));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
只需替换URL和令牌ID。
这可以通过邮递员生成。
一旦成功,您就可以相应地调整内容长度。