gpt4 book ai didi

php - 使用 Curl 在 PHP 中使用用户名和密码访问 API

转载 作者:行者123 更新时间:2023-12-05 04:10:14 24 4
gpt4 key购买 nike

这是示例代码,但在 cmd 上使用 Curl:

curl https://www.zopim.com/api/v2/chats -u {username}:{password}

我如何在 PHP 中查询它?

最佳答案

可能会在 How to use basic authorization in PHP curl 上回答所以从那里尝试:

<?php
$username='username';
$password='pass';
$URL='https://www.zopim.com/api/v2/chats';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);

打印或显示 json 使用:

$obj = json_decode($result);
print $obj->{'user'};
echo $obj->{'message'};

关于php - 使用 Curl 在 PHP 中使用用户名和密码访问 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44771350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com