发送数组UTF8 CURL

时间:2018-07-09 18:24:13

标签: php arrays curl

我正在尝试通过curl将这个数组发送到Facebook。

我正在尝试将带有按钮的消息发送给页面用户:

ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);
$userPageConversation = 'XXXX';
$Access_token = "XXXXX";    
if (isset($_GET['hub.mode']) && isset($_GET['hub.challenge']) && isset($_GET['hub.verify_token'])) {
    if ($_GET['hub.verify_token'] == 'gyt45h153581tg1hry94165151sd5v151s')
        echo $_GET['hub.challenge'];
} else {
$url = "https://graph.facebook.com/v2.6/".$userPageConversation."/messages?access_token=".$Access_token;

$ch = curl_init($url);

function utf8_converter($array)
{
    array_walk_recursive($array, function(&$item, $key){
        if(!mb_detect_encoding($item, 'utf-8', true)){
                $item = utf8_encode($item);
        }
    });
    return $array;
}
$variavel = array(
'message' => array(
'attachment' => array(
'type' => urlencode('template'),
'payload' => array(
    'template_type' => urlencode('button'),
    'text' => urlencode('Try the postback button!'),
    'buttons' => [array(
        'type' => urlencode('postback'),
        'title' => urlencode('Postback Button'),
        'payload' => urlencode('START')
    )]
))));
$fields_string = http_build_query(utf8_converter($variavel));
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array(
    'Content-type: application/json',
    'charset: utf-8',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields_string));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_exec($ch);
if($errno = curl_errno($ch)){
    $error_message = curl_strerror($errno);
    $erro =  "cURL error ({$errno}):\n {$error_message}";
    $exemplo = fopen('text.txt','w');
    fwrite($exemplo,$erro);
    fclose($exemplo);
}
curl_close($ch);
http_response_code(200);
}

但是我遇到了这个错误:

  

{“错误”:{“消息”:”(#100)参数主体必须为UTF-8编码   字符串”,“类型”:“ OAuthException”,“代码”:100,“ fbtrace_id”:“ HRFiDIbCLRs”}}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

尝试更换

$headers = array(
    'Content-type: application/json',
    'charset: utf-8',
);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

使用

$headers = array(
    'Content-Type: application/x-www-form-urlencoded',
    'charset=utf-8',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

答案 1 :(得分:0)

function SendRawResponse($Rawresponse){
    $userPageConversation = 't_100005050355547';
    $Access_token = "XXXX"; 
    $url = "https://graph.facebook.com/v2.6/".$userPageConversation."/messages?access_token=".$Access_token;
    //$url = "https://graph.facebook.com/v2.6/me/messages?access_token=".$Access_token;
    $ch = curl_init($url);
    $headers = array(
        'Content-Type: application/x-www-form-urlencoded',
        'charset=utf-8',
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $Rawresponse);
    curl_setopt($ch, CURLOPT_POST, true);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_exec($ch);
    $erros = curl_errno($ch);
    $response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    var_dump($erros);
    var_dump($response);
    curl_close($ch);
}

试图从我的页面向用户发送消息按钮