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'}; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?