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

use Curl to access API with username and password in PHP

我需要你的帮助...我如何在 php 上查询 这是示例代码,但在 cmd

上使用 Curl
https://www.zopim.com/api/v2/chats -u {username}:{password}

谢谢..

可能会在 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'};