2016-08-03 34 views
1

Hier versuche ich ein Formular mit php und restfull webservices einzuloggen, wann immer ich versuche, es auszuführen, öffnet grundlegender auth Benutzername und Passwort. Es muss automatisch kommunizieren, in meiner Situation passiert keine Idee, wo ich falsch liege?php Formular über basic authentication senden restfull api

<?php 

    if(isset($_POST['submit'])){ 

    $postData = array('userId'=>$_POST['userId'],'password'=> $_POST['password']); 


    $authToken = base64_encode("rest:rest"); 

    // Setup cURL 
    $ch = curl_init('http://xxxxxxxxxxx:8080/LIMS_Mobile/rest/loginpage/authenticate'); 
    curl_setopt_array($ch, array(
     CURLOPT_POST => TRUE, 
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_HTTPHEADER => array(
      'Authorization: Basic'.$authToken, 
      'Content-Type: application/json' 
     ), 
     CURLOPT_POSTFIELDS => json_encode($postData) 
    )); 

    // Send the request 
    $response = curl_exec($ch); 

    // Check for errors 
    if($response === FALSE){ 
     die(curl_error($ch)); 
    } 

    // Decode the response 
    $responseData = json_decode($response, TRUE); 

    // Print the date from the response 
    print_r($responseData); 




    if (!isset($_SERVER['PHP_AUTH_USER'])) { 
     header('WWW-Authenticate: Basic realm="My Realm"'); 
     header('HTTP/1.0 401 Unauthorized'); 
     echo 'Text to send if user hits Cancel button'; 
     exit; 
    } else { 
     echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>"; 
     echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>"; 
    } 
    } 
    ?> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title></title> 
    </head> 

    <body> 
    <form method="post" name="myForm" class="form-horizontal"> 
    <div class="row"> 
    <div class= "form-group"> 
     <label class= "col-md-2"> UserId </label> 
     <div class="col-md-4"> 
     <input name="userId" type= "text" class= "form-control" placeholder="Your name" required/> 
      </div> 
    </div> 
    <div class= "form-group"> 
     <label class= "col-md-2"> Passeord </label> 
     <div class="col-md-4"> 
     <input name="password" type= "text" class= "form-control" placeholder="Your name" required/> 
     </div> 
    </div> 

    <div class= "form-group"> 
     <label class= "col-md-2"></label> 
     <div class="col-md-4"> 

      <input type="submit" name="submit"/> 

     </div> 
    </div> 
    </div> 
    </form> 
    </body> 
    </html> 

Antwort

0

Um die Standardauthentifizierung mit curl Anfrage verwenden Sie sowohl die CURLOPT_HTTPAUTH und CURLOPT_USERPWD Optionen in der Anfrage verwendet werden soll:

$curl = curl_init(); 
.... 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC) ; 
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); 

Falls Sie die curl_setopt_array Funktion verwenden Sie so etwas tun kann :

$curl = curl_init(); 
curl_setopt_array($curl, [ 
    ... 
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 
    CURLOPT_USERPWD => "username:password", 
]); 

Beachten Sie, dass in Ihrem Code speziell Sie ein Leerzeichen zwischen Basic fehlten ein d das Token.

+0

$ ch = curl_init ('http: // xxxxxxx: 8080/LIMS_Mobile/rest/Loginseite/authentifizieren'); curl_setopt_array ($ ch, array ( CURLOPT_POST => TRUE, CURLOPT_RETURNTRANSFER => TRUE, \t CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_HTTPHEADER => Array ( 'Authorization: Basic' $ authToken, ‚Content-Type:. Anwendung/json ' ), CURLOPT_POSTFIELDS => json_encode ($ postData) )); Ich habe diese Änderung gemacht und ich habe es verstanden. danke für die Hilfe. Könntest du mir bitte erklären, warum es funktioniert hat !! – 3bu1