关于PHP解析API返回的json问题

时间:2018-10-02 05:45:07

标签: php json curl

首先,对不起我的英语不好,我使用了公司的文字审核API(用于过滤不符合当地法律法规的用户名),通过他,我可以获得一些项目的得分来区分是否违反,服务器的返回内容是json。我想通过读取“标签”的序列号来获得相应的分数,以供以下使用,但是有多个“标签”,并且如果有一项,它将不会以特定的顺序排列,因此我很难想到解决方案。 (主要问题:如何通过“标签”获得相应分数),谢谢您的耐心

<?php
function curl_post_https($url,$data) {
// Simulate submit data function
$curl = curl_init();
// Start a CURL session
curl_setopt($curl, CURLOPT_URL, $url);
// address to access
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
// Check the source of the certificate
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
// Check if the SSL encryption algorithm exists from the certificate
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
// Simulate the browser used by the user
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// use automatic jump
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
// Automatically set the Referer
curl_setopt($curl, CURLOPT_POST, 1);
// Send a regular Post request
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
// Post submitted packet
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
// Set the timeout limit to prevent infinite loops
curl_setopt($curl, CURLOPT_HEADER, 0);
// Display the contents of the returned Header area
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// The information obtained is returned as a file stream
$tmpInfo = curl_exec($curl);
// perform the operation
    if(curl_errno($curl)) {
        Echo 'Errno'.curl_error($curl);
     // catch the exception
    }
curl_close($curl);
//Close the CURL session
    return $tmpInfo;
    //return data, json format
}
$result_json = curl_post_https('https://aip.baidubce.com/rest/2.0/antispam/v2/spam',array('access_token' => '24.de91542f26612322bf152af17e24620c.2592000.1540919681.282335-14329580','content' = => '操你妈'));
$result = json_decode($result_json, true);
$log_id = $result["log_id"];
$spam = $result["result"]["spam"];
$review = $result["result"]["review"];
$reject = $result["result"]["reject"][0]["hit"];
$reject_echo = '';
for ($i = 0; $i < count($reject); $i++) {
    $reject_echo = $reject_echo.$reject[$i].' | ';
}
echo 'Server returns json:<br>'.$result_json.'<br>'.' Unique identifier: '.$log_id.'<br>'.'Text review result: '.$spam.'<br> ';
echo 'prohibited word list: '.$reject_echo.'<br>';
$pass_info = $result["result"]["pass"];
$pass_info_echo = '';
for ($i = 0; $i < count($pass_info); $i++) {
    $pass_info_echo = $pass_info_echo. 'Prohibited detection score: "'.$pass_info[$i]["score"].'" &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; Prohibited type: "'.$pass_info[ $i]["label"].'" <br>';
}
$reject_info = $result["result"]["reject"];
$reject_info_echo = '';
for ($i = 0; $i < count($reject_info); $i++) {
    $reject_info_echo = $reject_info_echo. 'Prohibited detection score: "'.$reject_info[$i]["score"].'" &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; Prohibited type: "'.$reject_info[ $i]["label"].'" <br>';
}
$review_info = $result["result"]["review"];
$review_info_echo = '';
for ($i = 0; $i < count($review_info); $i++) {
    $review_info_echo = $review_info_echo. 'Prohibited detection score: "'.$review_info[$i]["score"].'" &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; Prohibited type: "'.$review_info[ $i]["label"].'" <br>';
}
$baokongweijing_score = $pass_info[0]["score"];
$wenbenseqing_score = $pass_info[1]["score"];
$zhengziminggan_score = $pass_info[2]["score"];
$eyituiguang_score = $pass_info[3]["score"];
$dizhuruma_score = $pass_info[4]["score"];
$diziguansui_score = $pass_info[5]["score"];
the items passed by echo ' are as follows: <br>'.$pass_info_echo;
the items that echo 'does not pass are as follows:<br>'.$reject_info_echo;
echo 'The items that need to be reviewed are as follows:<br>'.$review_info_echo;
echo 'The scores of each item are as follows: <br>'.'Terrorism is prohibited: '.$baokongweijing_score.'<br>'.'Text porn: '.$wenbenseqing_score.'<br>'.' politically sensitive: '. $zhengziminggan_score.'<br>'.' Malicious promotion: '.$eyituiguang_score.'<br>'.'Low insult: '.$dizhuruma_score.'<br>'.'Low quality irrigation: '.$diziguansui_score;//Display its corresponding score

?>

错误应该在:

$baokongweijing_score = $pass_info[0]["score"];
$wenbenseqing_score = $pass_info[1]["score"];
$zhengziminggan_score = $pass_info[2]["score"];
$eyituiguang_score = $pass_info[3]["score"];
$dizhuruma_score = $pass_info[4]["score"];
$diziguansui_score = $pass_info[5]["score"];//Write the score to the function

因为他只检查了通过(pass),但是没有检测到拒绝/复审的选项,但是我不知道如何解决。

1 个答案:

答案 0 :(得分:0)

从API取回JSON时,您可以创建几个使用label字段作为数组索引的数组。这使用array_column()提取数据。

要确保所有元素都有值,首先要创建一个空白数组(使用array_fill())-您需要确保0,5创建您需要的所有元素(因此所有可能值label)。然后使用array_replace() ...

从JSON中添加值。
$result_json = curl_post_https('https://aip.baidubce.com/rest/2.0/antispam/v2/spam',array('access_token' => '24.de91542f26612322bf152af17e24620c.2592000.1540919681.282335-14329580','content' => '操你妈'));
$result = json_decode($result_json, true);
$rejects = array_fill(0,5,["score" => 0]);
$pass = array_fill(0,5,["score" => 0]);
$rejects = array_replace($rejects, array_column($result['result']['reject'], null, "label"));
$pass = array_replace($pass, array_column($result['result']['pass'], null, "label"));

print_r($rejects);
print_r($pass);

在这种情况下会输出...

Array
(
    [0] => Array
        (
            [score] => 0
        )

    [1] => Array
        (
            [score] => 0
        )

    [2] => Array
        (
            [score] => 0
        )

    [3] => Array
        (
            [score] => 0
        )

    [4] => Array
        (
            [score] => 0
        )

    [5] => Array
        (
            [score] => 1
            [hit] => Array
                (
                    [0] => 操你妈
                    [1] => 操你
                )

            [label] => 5
        )

)
Array
(
    [0] => Array
        (
            [score] => 0
        )

    [1] => Array
        (
            [score] => 0.0099999997764826
            [hit] => Array
                (
                )

            [label] => 1
        )

    [2] => Array
        (
            [score] => 0.7379999756813
            [hit] => Array
                (
                )

            [label] => 2
        )

    [3] => Array
        (
            [score] => 0
            [hit] => Array
                (
                )

            [label] => 3
        )

    [4] => Array
        (
            [score] => 0.27147358208955
            [hit] => Array
                (
                )

            [label] => 4
        )

    [6] => Array
        (
            [score] => 0.014000000432134
            [hit] => Array
                (
                )

            [label] => 6
        )

)

(如您所见,0.5不够,最后一项是6)