$v ) { $t = explode( ':', $v, 2 ); if( isset( $t[1] ) ) $head[ trim($t[0]) ] = trim( $t[1] ); else { $head[] = $v; if( preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#",$v, $out ) ) $head['response_code'] = intval($out[1]); } } return $head; } if(!isSet($_POST['key'])) exit("ERROR: NO KEY"); $key = $_POST['key']; if(!isSet($_POST['text'])) exit("ERROR: NO TEXT"); $text = $_POST['text']; $query = '{comment: {text:"' . $text . '"}, languages: ["en"], requestedAttributes: {TOXICITY:{},SEVERE_TOXICITY:{},IDENTITY_ATTACK:{},INSULT:{},PROFANITY:{},THREAT:{},SEXUALLY_EXPLICIT:{},FLIRTATION:{}}}'; $options = array('http' => array('method' => "POST", 'header' => "\r\nContent-type: application/json\r\nContent-Length: " . strlen($query) . "\r\n", 'content' => $query, 'ignore_errors' => true)); $context = stream_context_create($options); $result = file_get_contents("https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=$key", false, $context); if($result === false) { exit("ERROR: NETWORK FAILIURE"); } $resultHeaders = parseHeaders($http_response_header); if($resultHeaders['response_code'] >= 400) { if(isSet($result -> error -> message)) exit("ERROR: API ERROR: " . $result -> error -> message); exit("ERROR: API RETURNED CODE " . $resultHeaders['response_code']); } // Remove the spanscores in order to make the json parsing slightly faster on the torquescript side $result = json_decode($result); unset($result -> attributeScores -> TOXICITY -> spanScores); unset($result -> attributeScores -> SEVERE_TOXICITY -> spanScores); unset($result -> attributeScores -> IDENTITY_ATTACK -> spanScores); unset($result -> attributeScores -> INSULT -> spanScores); unset($result -> attributeScores -> PROFANITY -> spanScores); unset($result -> attributeScores -> THREAT -> spanScores); unset($result -> attributeScores -> SEXUALLY_EXPLICIT -> spanScores); unset($result -> attributeScores -> FLIRTATION -> spanScores); $result -> middlewareVersion = $perspectiveMiddlewareVersion; $result = json_encode($result, JSON_PRETTY_PRINT); print($result); ?>