Using cURL (in PHP) to access http s url is often not as simple as using the proper url. Utilizzando cURL (in PHP) per accedere a s url http spesso non è così semplice come con il corretto url. Using it for authentication is also not very clearly documented. Di utilizzare per l'autenticazione, inoltre, non è molto chiaramente documentate. This is a mini tutorial for both accessing https url’s as well as for http authentication. Si tratta di un mini tutorial sia per l'accesso a https URL e per l'autenticazione HTTP.

The following is a simple example which show the most common options you will ever need to use to access https url’s as well as for http authentication. Il seguente è un semplice esempio che mostra il più comune opzioni che si dovrà mai utilizzare per accedere a https URL e per l'autenticazione HTTP.

// The usual - init a curl session and set the url / / Il solito - init curl uno sessione e impostare l'url
$ch = curl_init(); $ ch = curl_init ();
curl_setopt($ch, CURLOPT_URL, $base_url); curl_setopt ($ ch, CURLOPT_URL, $ base_url);

// Set your login and password for authentication / / Imposta il tuo login e la password per l'autenticazione
curl_setopt($ch, CURLOPT_USERPWD, ‘login:pasword’); curl_setopt ($ ch, CURLOPT_USERPWD, 'login: password');

// You can use CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, / / È possibile utilizzare CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE,
// CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE / / CURLAUTH_NTLM, CURLAUTH_ANY, e CURLAUTH_ANYSAFE
// / /
// You can use the bitwise | (or) operator to combine more than one method. / / È possibile utilizzare il bit | (o) operatore di combinare più di un metodo.
// If you do this, CURL will poll the server to see what methods it supports and pick the best one. / / In questo caso, CURL sondaggio sarà il server per vedere quali metodi esso supporta e scegliere il migliore.
// / /
// CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | / / CURLAUTH_ANY è un alias per CURLAUTH_BASIC | CURLAUTH_DIGEST |
// CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM / / CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM
// / /
// CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | / / CURLAUTH_ANYSAFE è un alias per CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE |
// CURLAUTH_NTLM / / CURLAUTH_NTLM
// / /
// Personally I prefer CURLAUTH_ANY as it covers all bases / / Personalmente preferisco CURLAUTH_ANY in quanto copre tutte le basi
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt ($ ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

// This is occassionally required to stop CURL from verifying the peer’s certificate. / / Questo è talvolta necessario per fermare CURL dal verificare i peer certificato.
// CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if / / CURLOPT_SSL_VERIFYHOST può anche avere bisogno di TRUE o FALSE se
// CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a / / CURLOPT_SSL_VERIFYPEER è disabilitata (il default è 2 - controllare l'esistenza di un
// common name and also verify that it matches the hostname provided) / / Nome comune e verificare inoltre che corrisponda al nome host fornito)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false);

// Optional: Return the result instead of printing it / / Opzionale: Torna il risultato invece di stamparlo
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);

// The usual - get the data and close the session / / Il solito - ottenere i dati e chiudere la sessione
$data = curl_exec($ch); $ data = curl_exec ($ ch);
curl_close($ch); curl_close ($ ch);

Use the above as a template for your code to simplify your data access using cURL. Utilizzare il sopra come un modello per il tuo codice per semplificare il vostro accesso ai dati utilizzando l'arricciatura.

PS. The challenge with cURL documentation in PHP is that it is hard to find what you need from hundreds of available options and without enough examples of common use cases. La sfida con curl documentazione in PHP è che è difficile trovare di che cosa avete bisogno di centinaia di opzioni disponibili e senza abbastanza esempi di casi d'uso comune. What is needed is a series of How-To’s like the mini-tutorial above. Ciò che è necessario è una serie di How-To come il mini-tutorial di cui sopra.