【问题标题】:use Curl to access API with username and password in PHP在 PHP 中使用 Curl 使用用户名和密码访问 API
【发布时间】:2017-11-29 23:31:33
【问题描述】:

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

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

谢谢..

【问题讨论】:

  • curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  • @ivor 这个 API 是在 post 还是 get 方法上?我认为是帖子,对吧?
  • hmm.. 我需要获取此链接的 json...
  • @ivor 但是这个 API 使用的是哪种方法?发布或获取兄弟!
  • @duongkhang 获取方法兄弟...

标签: php json url zendesk-api


【解决方案1】:

也许在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'}; 

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 2015-03-13
    • 2012-08-09
    • 2015-03-27
    • 2012-06-29
    • 2019-01-30
    • 2018-06-05
    • 2011-02-05
    • 1970-01-01
    相关资源
    最近更新 更多