2014-01-10 39 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”);

而不是一个数组它需要一个字符串。