【问题标题】:php curl to self signed certificate urlphp curl 到自签名证书 url
【发布时间】:2014-01-28 23:14:34
【问题描述】:

我不是 php 开发人员!我只是想让 php curl 在我的本地主机上工作,与我的 REST 服务交谈......

注意:我有一个自签名证书,我怀疑 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 Continue”?不应该是这样的!这是我看到的回应。

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":"...."}

【问题讨论】:

    标签: php curl ssl


    【解决方案1】:

    找到问题了!

    更改以下行有效!

    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "grant_type=password&client_id=....&client_secret=....&scope=play,trust&username=tester&password=121212");

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

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2021-02-20
      • 2020-09-20
      • 2021-08-07
      • 1970-01-01
      • 2015-02-21
      • 1970-01-01
      • 2014-02-06
      • 2015-05-23
      相关资源
      最近更新 更多