【发布时间】: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);
【问题讨论】: