douwen4125 2017-06-27 02:16
浏览 410
已采纳

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

I need your help... how do I query it on php this is the sample code but using Curl on cmd

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

Thanks..

  • 写回答

1条回答 默认 最新

  • doubu2730 2017-06-27 02:24
    关注

    maybe answered on How to use basic authorization in PHP curl so try from that:

    <?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);
    

    to print or show the json use:

    $obj = json_decode($result);
    print $obj->{'user'}; 
    echo $obj->{'message'}; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料