2011-10-20 10 views
5

Sto lavorando con l'API di Google Traduttore e c'è la possibilità che io possa inviare un po 'di testo da tradurre. In questo scenerio Google raccomanda di effettuare le seguenti operazioni:Come faccio a fare un POST usando X-HTTP-Method-Override con una richiesta di curl PHP?

È anche possibile utilizzare POST per richiamare l'API se si desidera inviare più dati in una singola richiesta. Il parametro q nel corpo POST deve essere inferiore a di 5 caratteri. Per utilizzare POST, è necessario utilizzare l'intestazione X-HTTP-Method-Override per indicare all'API Translate di trattare la richiesta come GET (utilizzare X-HTTP-Method-Override: GET). Google Translate API Documentation

so come fare una richiesta POST normale con CURL:

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
$response = curl_exec($curl); 
curl_close($curl); 
echo $response; 

Ma come faccio a modificare l'intestazione di utilizzare la X-HTTP-Metodo-override?

risposta

6
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET')); 
+0

Perfezionare! Questo è esattamente ciò di cui avevo bisogno. – ashansky

+0

Questo lavoro ha funzionato? Sto ancora ricevendo BAD REQUEST. ; ( – tofutim

0

usare l'opzione CURLOPT_HTTPHEADER per aggiungere un'intestazione da una matrice di stringhe

1

http://php.net/manual/en/function.curl-setopt.php

CURLOPT_HTTPHEADER

Un array di campi di intestazione HTTP per impostare, nel formato array('Content-type: text/plain', 'Content-length: 100')

Quindi,

curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET')); 
0

Non abbastanza per me, ho bisogno di usare http_build_query fo i miei dati post matrice mia piena esempio:

$param = array(
    'key' => 'YOUR_API_KEY_HERE', 
    'target' => 'en', 
    'source' => 'fr', 
    "q" => 'text to translate' 
    ); 
    $formData = http_build_query($param); 
    $headers = array("X-HTTP-Method-Override: GET"); 
    $ch=curl_init(); 

    curl_setopt($ch, CURLOPT_POST, 1); 

    curl_setopt($ch, CURLOPT_POSTFIELDS,$formData); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); 
    curl_setopt($ch, CURLOPT_REFERER, 'http://yoursite'); //if you have refere domain restriction for your google API KEY 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_URL,'https://www.googleapis.com/language/translate/v2'); 
    $query = curl_exec($ch); 
    $info = curl_getInfo($ch); 
    $error = curl_error($ch); 
    $data = json_decode($query,true); 

    if (!is_array($data) || !array_key_exists('data', $data)) { 
    throw new Exception('Unable to find data key'); 
    } 
    if (!array_key_exists('translations', $data['data'])) { 
    throw new Exception('Unable to find translations key'); 
    } 
    if (!is_array($data['data']['translations'])) { 
    throw new Exception('Expected array for translations'); 
    } 
    foreach ($data['data']['translations'] as $translation) { 
    echo $translation['translatedText']; 
    } 

ho trovato questo aiuto qui https://phpfreelancedeveloper.wordpress.com/2012/06/11/translating-text-using-the-google-translate-api-and-php-json-and-curl/ Speranza che aiuta

Problemi correlati