【问题标题】:How to Retrieve cURL Data within the URL Endpoint如何在 URL 端点中检索 cURL 数据
【发布时间】:2022-01-21 11:05:09
【问题描述】:

我正在尝试创建一个 cURL api,假设下面是我的 curl 请求代码,其中 http://localhost/api/endpoint.php 是我的端点。在 endpoint.php 文件中,代码方面实际上会发生什么?如何在 endpoint.php 页面中实际获取通过 cURL 请求发送的数据,例如 CURLOPT_USERPWD 数据?谢谢。

$ch = curl_init();
$url = 'http://localhost/api/endpoint.php';

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, 'alex:password123');

$headers = array(
        'Accept: application/json',
        'Content-Type: application/x-www-form-urlencoded',
        'Accept-Language: en_US'        
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$vars['grant_type'] = 'client_credentials';

$req = http_build_query($vars);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);

$response = curl_exec($ch);

【问题讨论】:

    标签: php curl endpoint


    【解决方案1】:

    您可以通过$_SERVER superglobal访问这些数据。

    端点:

    <?php
    
    $user     = $_SERVER['PHP_AUTH_USER']; // alex
    $password = $_SERVER['PHP_AUTH_PW'];   // password123
    

    【讨论】:

    • 啊,我明白了,太好了,谢谢!
    【解决方案2】:

    在您的文件 endpoint.php 中,您可以使用

    打印您发布的值
    print_r($_POST);
    

    在你的卷曲打印响应之后

    print_r($response);
    

    【讨论】:

    • 感谢您的回复,但是 print_r($_POST) 仅显示通过 CURLOPT_POSTFIELDS 发送的值 - 在这种情况下 $vars['grant_type'] = 'client_credentials';它不显示 CURLOPT_USERPWD 凭据。但是,使用 Syscall 提到的 $_SERVER 超级全局,您可以获得用户和密码,谢谢。
    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 2015-10-24
    • 2018-09-08
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2015-10-05
    相关资源
    最近更新 更多