【问题标题】:Send array UTF8 CURL发送数组 UTF8 CURL
【发布时间】:2018-07-09 18:24:12
【问题描述】:

我正在尝试通过 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);
}

但我收到此错误:

{"error":{"message":"(#100) 参数正文必须是 UTF-8 编码 string","type":"OAuthException","code":100,"fbtrace_id":"HRFiDIbCLRs"}}

谁能帮帮我?

【问题讨论】:

    标签: php arrays curl


    【解决方案1】:

    尝试替换

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

    【讨论】:

    • {"error":{"message":"(#501) 缺少消息体","type":"OAuthException","code":501,"fbtrace_id":"HX2KPyHxEiV"} }
    • 尝试将curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields_string));换成curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields_string));
    • curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($Rawresponse)); = 缺少消息正文 ||||||
    • curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($Rawresponse)); = 警告:http_build_query():参数 1 应为数组或对象。给定的值不正确 |||||
    • curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 这段代码有什么用?
    【解决方案2】:
    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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2013-04-02
      • 2012-05-03
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      相关资源
      最近更新 更多