2016-07-07 4 views
0

Ich muss POST-Methode mit curl senden, aber beim Öffnen $ URL hat ein Popup-Fenster mit Benutzername/Passwort zu übermitteln (wie mit htpasswd).PHP - mit curl, wie man Benutzername Passwort in einem Popup von htpasswd senden?

Aus irgendeinem Grund der folgende Code i bekommen funktioniert nicht vom Server

HTTP/1.1 401 Unauthorized and HTTP/1.1 404 Not Found (https://www.httpwatch.com/httpgallery/authentication/)

// For Debug server response details 
$curlOptions = array(
    CURLOPT_RETURNTRANSFER => TRUE, 
    CURLOPT_FOLLOWLOCATION => TRUE, 
    CURLOPT_VERBOSE => TRUE, 
    CURLOPT_STDERR => $verbose = fopen('php://temp', 'rw+'), 
    CURLOPT_FILETIME => TRUE, 
); 

// Real meat 
$ch = curl_init(); 
curl_setopt_array($ch, $curlOptions); 
curl_setopt($ch, CURLOPT_URL,$URL); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 

// is this the right way here in POST method??? 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");// is this correct way to enter username/password in the popup? 

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
$result=curl_exec ($ch); 

// Pretty print the debug logs 
echo '<pre>', 
     !rewind($verbose), 
     stream_get_contents($verbose), 
     "\n", 
    '</pre>'; 

curl_close ($ch); 
echo $result; 

Wie kann ich meine POST-Methode Vorlage stellen Sie sicher, wirklich das Benutzername/Passwort einreicht? (Debug-Protokoll ich sehe nicht, wenn ich mich richtig abgegeben haben, wird Server auch von mir nicht erhalten, da seine Drittanbieter-Dienstleister API)

EDIT:

TRY1: failed

// For Debug server response details 
$curlOptions = array(
    CURLOPT_RETURNTRANSFER => TRUE, 
    CURLOPT_FOLLOWLOCATION => TRUE, 
    CURLOPT_VERBOSE => TRUE, 
    CURLOPT_STDERR => $verbose = fopen('php://temp', 'rw+'), 
    CURLOPT_FILETIME => TRUE, 
); 

// Real meat 
$ch = curl_init(); 
curl_setopt_array($ch, $curlOptions); 


// 
// 
// 
// 
// STACK-OVERFLOW EXPERT SAID UPDATE THE $URL WITH USER:[email protected] THIS 
// 
// 
// 
//  

$URL = "http://$username:[email protected]/postblabla"; 
curl_setopt($ch, CURLOPT_URL, $URL); 



curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 


// 
// 
// 
// 
// STACK-OVERFLOW EXPERT SAID REMOVE THIS 
// 
// 
// 
//  
//curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
$result=curl_exec ($ch); 

// Pretty print the debug logs 
echo '<pre>', 
     !rewind($verbose), 
     stream_get_contents($verbose), 
     "\n", 
    '</pre>'; 

curl_close ($ch); 
echo $result; 

Antwort

1

Mit HTTP Auth, Sie einen Benutzernamen und ein Passwort als Teil der URL senden:

<schema>://<username>:<password>@<url> 

auch this SO question and answer sehen.

Allerdings sollte die CURLOPT_USERPWD funktioniert haben und beide Methoden nur HTTP Authorization Header gesetzt. as mentioned here. Dies ist auch sicherer als Ihre Anmeldeinformationen werden nicht als Klartext in URL übertragen und mit der potenziellen Verwendung von SSL/TLS werden sie nicht sichtbar im Freien sein.

Auch das Festlegen von HTTP Basic auth kann helfen.

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
+0

Also, was Sie sagen, mein Code sollte diese Zeile entfernen? 'curl_setopt ($ ch, CURLOPT_USERPWD," $ username: $ password ");' und in der $ URL sollte ich den '$ username: $ password @ $ real_url' hinzufügen? – YumYumYum

+0

@YumYumYum genau. – Finwe

+0

Mein ursprünglicher Code und Ihr vorgeschlagener Code. Tun noch genau die gleichen Ergebnisse. siehe meine oben EDIT Abschnitt – YumYumYum

Verwandte Themen