繁体   English   中英

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

[英]use Curl to access API with username and password in PHP

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

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

如何在 PHP 中查询它?

也许回答了如何在 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'}; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM