2010-01-26 14 views

risposta

277

Si vuole in questo modo:

curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password); 

Zend ha un client REST e zend_http_client e sono sicuro che PEAR ha una sorta di involucro. Ma è abbastanza facile da fare da soli.

Così l'intera richiesta potrebbe essere simile a questo:

$process = curl_init($host); 
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders)); 
curl_setopt($process, CURLOPT_HEADER, 1); 
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password); 
curl_setopt($process, CURLOPT_TIMEOUT, 30); 
curl_setopt($process, CURLOPT_POST, 1); 
curl_setopt($process, CURLOPT_POSTFIELDS, $payloadName); 
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE); 
$return = curl_exec($process); 
curl_close($process); 
+4

Il cliente Zend resto è qui: http://framework.zend.com/manual/en/zend .rest.client.html –

+1

Cool thanks Pekka –

+0

@Pekka - il tuo link è rotto perché ti manca http: // –

7

A differenza di SOAP, REST non è un protocollo standardizzato, quindi è un po 'difficile avere un "client REST". Tuttavia, poiché la maggior parte dei servizi RESTful utilizza HTTP come protocollo sottostante, dovresti essere in grado di utilizzare qualsiasi libreria HTTP. Oltre ad arricciarsi, PHP ha questi tramite PEAR:

HTTP_Request2

che ha sostituito

HTTP_Request

Un esempio di come lo fanno HTTP Auth di base

// This will set credentials for basic auth 
$request = new HTTP_Request2('http://user:[email protected]/secret/'); 

Il supporto anche Digest Auth

// This will set credentials for Digest auth 
$request->setAuth('user', 'password', HTTP_Request2::AUTH_DIGEST); 
+0

Per client REST intendo qualcosa che astrae alcuni dei dettagli di basso livello dell'utilizzo di curl per http ottenere , post, inserisci, cancella ecc., che è quello che sto facendo costruendo la mia classe php per farlo, mi chiedo se qualcuno lo abbia già fatto. – blank

+1

Sì, allora HTTP_Request_2 potrebbe interessarti. via molto il più brutto di cUrl in PHP Per impostare il metodo che si usa, setMethod (HTTP_Request2 :: METHOD_ *). Con PUT e POSTs, per impostare il corpo della richiesta basta setBody (<< your xml, json, ecc. rappresentazione qui >>). Autenticazione descritta sopra.Esso ha anche astrazioni per la risposta HTTP (qualcosa che cUrl manca davvero.) – nategood

78

CURLOPT_USERPWD invia fondamentalmente le base64 della stringa user:password con l'intestazione http come di seguito:

Authorization: Basic dXNlcjpwYXNzd29yZA== 

Quindi a parte il CURLOPT_USERPWD è anche possibile utilizzare l'opzione HTTP-Request intestazione così come di seguito con altre intestazioni:

$headers = array(
    'Content-Type:application/json', 
    'Authorization: Basic '. base64_encode("user:password") // <--- 
); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
+0

Grazie, Sabuj. – frops

+0

Questo metodo di passare un'intestazione dell'autorizzazione personalizzata invece di usando 'CURLOPT_USERPWD' ha funzionato per me. – aalaap

23

Il modo più semplice e nativo è utilizzare CURL direttamente.

questo funziona per me:

<?php 
$login = 'login'; 
$password = 'password'; 
$url = 'http://your.url'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password"); 
$result = curl_exec($ch); 
curl_close($ch); 
echo($result); 
+0

Risposta molto utile, grazie. –

+0

Questo ha funzionato bene! +1 –

+0

Sì, questo è semplice e al punto. lo adoro –

2

Michael Dowling's molto attivamente mantenuto Guzzle è un buon modo per andare. A parte l'elegante interfaccia, chiamata asincrona e la conformità PSR, rende le intestazioni di autenticazione per il riposo chiama morto semplice:

// Create a client with a base URL 
$client = new GuzzleHttp\Client(['base_url' => 'http://myservices.io']); 

// Send a request to http://myservices.io/status with basic authentication 
$response = $client->get('/status', ['auth' => ['username', 'password']]); 

Vedi the docs.

0

Se il tipo di autorizzazione è di base autenticazione e dati inviati sono JSON poi fare come questo

<?php 

$data = array("username" => "test"); // data u want to post                 
$data_string = json_encode($data);                     
$api_key = "your_api_key"; 
$password = "xxxxxx";                             
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://xxxxxxxxxxxxxxxxxxxxxxx");  
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POST, true);                 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_USERPWD, $api_key.':'.$password); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 
    'Accept: application/json', 
    'Content-Type: application/json')               
);    

if(curl_exec($ch) === false) 
{ 
    echo 'Curl error: ' . curl_error($ch); 
}                          
$errors = curl_error($ch);                            
$result = curl_exec($ch); 
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 
echo $returnCode; 
var_dump($errors); 
print_r(json_decode($result, true)); 
Problemi correlati