2012-11-02 10 views
5

cercando di fare l'equivalente di questo in PHP - e in mancanza :):L'invio di autenticazione nelle intestazioni PHP arricciare

curl -H "X-abc-AUTH: 123456789" http://APIserviceProvider=http://www.cnn.com; 

"123456789" è la chiave API. L'istruzione della riga di comando funziona correttamente.

codice PHP (non funziona):

$urlToGet = "http://www.cnn.com"; 
$service_url = "http://APIserviceProvider=$urlToGet"; 

//header 

$contentType = 'text/xml';   //probably not needed 
$method = 'POST';     //probably not needed 
$auth = 'X-abc-AUTH: 123456789'; //API Key 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $service_url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLINFO_HEADER_OUT, true); 

//does not work 



// curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: ' . 
    // $contentType . '; auth=' . $auth)); 

    //works! (THANKS @Fratyr for the clue): 

    curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth)); 

//this works too (THANKS @sergiocruz): 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Some_custom_header: 0', 
    'Another_custom_header: 143444,12' 
)); 


//exec 

$data = curl_exec($ch); 
echo $data; 
curl_close($ch); 

Tutte le idee?

+1

Perché stai usando ' Content-Type' se il tuo esempio di riga di comando ha un'intestazione 'X-abc-AUTH:'? – mario

+0

Stavo ricevendo un errore "tipo di contenuto richiesto". Ma ho appena capito! Ho aggiornato il codice sopra. – ven

risposta

13

Al fine di ottenere le intestazioni personalizzate nella vostra arricciatura si dovrebbe fare qualcosa di simile al seguente:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Some_custom_header: 0', 
    'Another_custom_header: 143444,12' 
)); 

quindi il seguente dovrebbe funzionare nel tuo caso (dato X-abc-AUTH è l'unico colpo di testa è necessario inviare oltre):

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-abc-AUTH: 123456789' // you can replace this with your $auth variable 
)); 

Se avete bisogno di intestazioni personalizzate aggiuntive, tutto quello che dovete fare è aggiungere al matrice all'interno della curl_setopt.

Spero che questo aiuta :)

+1

Grazie amico questo ha funzionato benissimo! – ven

+0

Nessun problema :) – sergiocruz

0
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'auth=' . $auth 
)); 
+0

Questo non è nemmeno un http valido. – Evert

+0

Allora perché lo stesso post sopra ha ottenuto + e ho un -? :) – deb0rian

+0

Questo ha funzionato quasi! Ciò ha funzionato: curl_setopt ($ ch, CURLOPT_HTTPHEADER, Array ($ auth)); – ven

0

Si imposta una sola intestazione di richiesta, non i due che si voleva. Si potrebbe fare per esempio come questo:

// input 
$urlToGet = "http://www.cnn.com"; 

// url 
$service_url = sprintf("http://APIserviceProvider=%s", urlencode($urlToGet)); 

//header 
$contentType = 'Content-type: text/xml'; //probably not needed 
$auth  = 'X-abc-AUTH: 123456789'; //API Key 
$method  = 'POST'; //probably not needed 

// curl init 
$ch = curl_init($service_url); 
curl_setopt_array($ch, [ 
    CURLOPT_RETURNTRANSFER => true, 
    CURLINFO_HEADER_OUT => true, 
    CURLOPT_HTTPHEADER  => [ 
     $contentType, 
     $auth, 
    ], 
]); 

// curl exec 
$data = curl_exec($ch); 
curl_close($ch); 

// output 
echo $data; 

(modificare l'URL di servizio a quella destra per arrivare a questo lavoro)

+0

grazie per il tuo suggerimento - sembra che avrebbe funzionato ma non ha funzionato: (Ho cambiato anche il servizio url + key. ..thanks again – ven

+0

Si potrebbe voler eseguire un controllo degli errori dopo 'curl_exec':' var_dump (curl_error ($ ch)); ' – hakre

3

utilizzare la seguente sintassi

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$headers = array(); 
$headers[] = 'X-abc-AUTH: 123456789'; 
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; 
$headers[] = 'Accept-Encoding: gzip, deflate'; 
$headers[] = 'Accept-Language: en-US,en;q=0.5'; 
$headers[] = 'Cache-Control: no-cache'; 
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8'; 
$headers[] = 'Host: 202.71.152.126'; 
$headers[] = 'Referer: http://www.example.com/index.php'; //Your referrer address 
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0'; 
$headers[] = 'X-MicrosoftAjax: Delta=true'; 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$server_output = curl_exec ($ch); 

curl_close ($ch); 

print $server_output ; 
Problemi correlati