2014-01-10 40 views
0

我不是一個php開發人員!我只是想讓php curl在我的本地主機上工作,與我的REST服務通話......php curl自簽名證書url

注:我有一個自簽名證書,我懷疑php curl會轉到[http:// localhost :8443/myapp/oauth/token]而不是[https:// localhost:8443/myapp/oauth/token]

但我想不通爲什麼?

這是我的代碼如下;

public function oauth2() { 

    echo 'Starting...<br/>'; 

    $postargs = array(
      'grant_type' => 'password', 
      'client_id' => 'some-client', 
      'client_secret' => 'some-secret', 
      'scope' => 'play,trust', 
      'username' => 'tester', 
      'password' => '121212' 
    ); 
    $url = 'https://localhost:8443/myapp/oauth/token'; 

    try { 
     $this->load->library('curl'); 
     $curl_handle = curl_init(); 
     curl_setopt($curl_handle, CURLOPT_URL, $url); 
     curl_setopt($curl_handle, CURLOPT_PORT, 8443); 
     curl_setopt($curl_handle, CURLOPT_POST, true); 
     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl_handle, CURLOPT_HEADER, true); 
     curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json')); 
     curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt($curl_handle, CURLOPT_SSLVERSION, 3); 
     curl_setopt($curl_handle, CURLOPT_NOBODY, true); 
     //curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); 
     //curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 10); 
     //curl_setopt($curl_handle, CURLOPT_VERBOSE, true); 
     curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postargs); 

     $buffer = curl_exec($curl_handle); 
     if (curl_errno($curl_handle)) { 
      echo curl_error($curl_handle).'<br/>'; 
     } else { 
      //echo curl_getinfo($curl_handle); 
      curl_close($curl_handle); 
      echo '<p>'.$buffer.'</p>'; 

     } 

    } catch (Exception $e) { 
     echo 'Exception....<br/>'; 
     echo $e->getMessage(); 
     echo $e->getTraceAsString(); 
     die; 
    } 
    echo 'Continue...<br/>'; 
    die; 
} 

以上沒有工作...爲什麼我在開始的時候得到 「HTTP/1.1 100繼續」?它不應該是這樣的!這是我看到的迴應。

HTTP/1.1 100 Continue HTTP/1.1 401 
Unauthorized Server: Apache-Coyote/1.1 
Cache-Control: no-store 
Pragma: no-cache 
WWW-Authenticate: Basic realm="***/client", 
error="unauthorized", 
error_description="An Authentication object was not found in the SecurityContext" 
Content-Type: application/json; 
charset=UTF-8 Transfer-Encoding: chunked Date: Fri, 
10 Jan 2014 04:44:40 GMT 
{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"} 

但是,當我從下面的命令行運行curl它的作品!

curl -k -i -H "Accept: application/json" -X POST -d "grant_type=password&client_id=some-client&client_secret=some-secret&scope=play,trust&username=tester&password=121212" [https://localhost:8443/myapp/oauth/token] 

HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1 
Cache-Control: no-store 
Pragma: no-cache 
Content-Type: application/json;charset=UTF-8 
Transfer-Encoding: chunked 
Date: Fri, 10 Jan 2014 04:56:45 GMT 

{"access_token":"cee03c37-9168-4dca-8ffe-bd7f469cdcb5","token_type":"bearer","expires_in":5238,"scope":"play trust","client_id":"...."} 

回答

0

發現問題了!

更改線下工作!

curl_setopt($ curl_handle,CURLOPT_POSTFIELDS, 「grant_type =密碼&的client_id = .... & client_secret = .... &範圍=玩,信任&用戶名=測試&密碼= 121212」);

而不是一個數組它需要一個字符串。