将curl转换为CakePHP 3.6 httpclient?

时间:2018-05-01 10:02:02

标签: php curl cakephp

我正在使用BShaffer OAuth2 / CakePHP 3.6以及大部分方式。但是,我在代码中遇到了一个小问题,我正在尝试将PHP Curl命令转换为Cake友好的httpClient命令

有问题的curl命令是

curl -u testclient:testpass http://localhost/token.php -d 'grant_type=client_credentials'

而我目前对代码的尝试是

$http = new Client();

$auth = [
    '_csrfToken' => $this->request->getParam('_csrfToken'),
    'grant_type' => 'client_credentials',
    'username' => $result->UserID,
    'password' => $createCode
];

$response = $http->post('/oauth/request', $auth);

我有一种感觉我需要将用户名/密码放入OAuth2的auth标头中,但我不是100%肯定如何这样做。 httpClient的文档包含OAuth2,但作为单个标题:

$http = new Client([
'headers' => ['Authorization' => 'Bearer ' . $accessToken]
]);
$response = $http->get('https://example.com/api/profile/1');

编辑:

通过评论中的示例,我已经让PHP的CURL直接工作,但Cake的客户端仍然产生空白结果。这就是我现在所拥有的:

$data = $this->request->getData();

$result = $this->Users->RegisterNewUser($data);
$report = $data['email'];

if ($result->result == 1)
{
    $generatePrivateKey = $this->OAuth->generatePrivateKey($result->UserID);


    $query = [
        'client_id' => $result->UserID,
        'client_secret' => $generatePrivateKey,
        'redirect_uri' => ''
    ];
    $this->OAuth->insertClientData($query);


// Insert User into clients //
    $auth = [
        'grant_type' => 'client_credentials'
    ];
    // Is not working //
    $http = new Client();
    $response = $http->post('https://example.com/oauth/request', $auth, [
      'auth' => ['username' => $result->UserID, 'password' => 
$generatePrivateKey]
    ]);
    print_r($response->getBody());

    // Works //
    $ch = curl_init('https://example.com/oauth/request');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $auth);        
    curl_setopt($ch, CURLOPT_USERPWD, $result->UserID . ":". $generatePrivateKey);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $output = curl_exec($ch);
    curl_close($ch); 
}

2 个答案:

答案 0 :(得分:1)

如评论中所述,您需要发出使用基本身份验证的请求,这就是显示的Curl请求所执行的操作。

为此,请在username选项中指定passwordauth键,该选项将在客户端post()方法的第三个参数中传递:< / p>

$http = new Client();

$data = [
    'grant_type' => 'client_credentials',
];

$options = [
    'auth' => [
        'username' => $result->UserID,
        'password' => $createCode
    ]
];

$response = $http->post('/oauth/request', $data, $options);

然后可以通过响应body()方法,流getContents()方法或甚至通过魔术属性(不是它们的粉丝)来检索响应主体,从而相应地解析数据: / p>

$stringBody = $reponse->body();
$stringBody = $response->getBody()->getContents();
$arrayData = $response->json;

另见

答案 1 :(得分:0)

尝试将此作为起点,它应该适用于您需要身份验证令牌的大多数cURL场景:

        $post_data = ["stuff" => $stuff, "stuff2" => $stuff2];

        $data_string = json_encode($post_data);
        $ch = curl_init('https://endpoint.com');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer ' . $your_auth_token,
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
        $output = curl_exec($ch);
        // Do stuff with output