我需要帮助使用PHP将Postman的成功请求转换为CURL命令(涉及HTTPS和POST) [英] I need help converting a successful request from Postman into CURL commands using PHP (involves HTTPS and POST)

查看:728
本文介绍了我需要帮助使用PHP将Postman的成功请求转换为CURL命令(涉及HTTPS和POST)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要覆盖的端点需要HTTPS和基本身份验证。我的团队获得了一个API密钥,文档声明使用密钥作为用户名,并将密码留空。

The endpoint I'm trying to reach requires HTTPS and Basic Authentication. My team was given an API key, and the documentation states to use the key as the username, and to leave the password blank.

以下是来自文档:

curl -i -k -u '<api_key>': -XPOST --data-urlencode data@/path/to/test/file.json "https://<your_subdomain>.vendor.org/api/v1/assessments/import"; echo ""

当我使用Chrome的Postman扩展程序执行以下操作时,我收到了成功的回复服务器:

When I execute the following using the Postman extension for Chrome, I get a successful response from the server:

我正在尝试使用PHP(XAMPP安装)在本地执行此操作。以下是服务器的回复,说用户名/密码不正确:

I'm trying to execute this locally using PHP (XAMPP install). The following is getting a response from the server saying the username/password is incorrect:

function curlPost($url, $headers, $username, $password, $data) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_CAINFO, 'certificate.pem');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	print_r(curl_exec($ch));
	// print_r(curl_getinfo($ch));
	// print_r(curl_error($ch));
	curl_close($ch);
}

$data = '{"key":"value", "key":"value"}';
curlPost('https://domain.com/api/data', ['Content-Type: application/xml'], 'api_key', '', $data);

{"success":false,"errors":["Email\/Username or password incorrect. Please try again."],"warnings":[],"info":[],"meta":[],"results":[]}

$ data 中使用的JSON字符串是从成功的Postman请求中复制并粘贴的。

The JSON string used in $data was copied and pasted from a successful Postman request.

certificate.pem与脚本位于同一文件夹中,并且已向每个人提供读/写权限。我已尝试从我的计算机导出供应商网站的特定证书,以及在顶部响应中链接的CA包这篇文章。我能够使用它通过PHP / CURL成功点击供应商的api-key-test端点。

The certificate.pem is in the same folder as the script, and read/write permissions have been given to everyone. I have tried exporting the specific certificate for our vendor's site from my machine as well as the CA bundle linked in the top response to this post. I was able to use it to successfully hit the vendor's api-key-test endpoint via PHP/CURL.

我对此很新。你介意帮助我绕过我所缺少的东西吗?虽然我复制并粘贴了很多,但功能基本上都是我自己的。标题的参数将用于其他内容。

I'm pretty new to this. Would you mind helping me wrap my head around what I'm missing? While I've copied and pasted a ton, the function is largely my own. The parameter for headers will be used for other things.

推荐答案

使用HTTP Authorization标头的基本身份验证使用Base64编码的值username:password(不带双引号)

Basic Authentication with the HTTP Authorization header uses the Base64 encoded value of "username:password" (without the double quotes)

所以我假设您需要Base64编码yourApiKeyValue:并将其放入cURL命令的Authorization标头中

So I'm assuming in your case you would need to Base64 encode "yourApiKeyValue:" and put that in a Authorization header in your cURL command

MDN参考 - HTTP身份验证

编辑:这也可能有帮助

如何使用http basic authentication-with-php-curl发出请求

这篇关于我需要帮助使用PHP将Postman的成功请求转换为CURL命令(涉及HTTPS和POST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆