这是示例代码,但在 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/