2016-08-03 7 views
1

ここでは、基本的な認証のユーザ名とパスワードをポップアップして実行するときに、phpとrestfull webservicesを使用してフォームにログインしようとしています。それは自動的に通信する必要があります。私の状況では、どこが間違っているのかわからないのですか?あなたが要求してCURLOPT_HTTPAUTHCURLOPT_USERPWDオプションの両方を使用する必要がありますカール要求に基本認証を使用するためにはphpフォーム基本認証を介して送信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> 

答えて

0

あなたはそのような何かを行うことができます curl_setopt_array機能を使用している場合は
$curl = curl_init(); 
.... 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC) ; 
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); 

:特にあなたのコードでは、あなたがBasic間にスペースを欠落していた

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

注意dトークン。

+0

$ ch = curl_init( 'http:// xxxxxxx:8080/LIMS_Mobile/rest/loginpage/authenticate'); curl_setopt_array($ chの、配列( CURLOPT_POST => TRUE、 CURLOPT_RETURNTRANSFER => TRUE、 \t CURLOPT_HTTPAUTH => CURLAUTH_BASIC、 CURLOPT_HTTPHEADER =>配列( '認証:ベーシック'。$ authTokenの、 「コンテンツタイプ:アプリケーション/ json ' )、 CURLOPT_POSTFIELDS => json_encode($ postData) ));私はこの変更を行い、私はそれを得た。手伝ってくれてありがとう。なぜそれが働いたのか説明していただけますか?!! – 3bu1