在服务器上进行身份验证时出错,但在获得答案的情况下在Postman中进行身份验证

时间:2020-02-27 18:26:29

标签: php curl

下午好,我试图建立与服务器的连接,为此,我必须传递访问凭据(用户名和密码)和XML文件。我的访问数据是校对员,但是服务器拒绝了我,我与Postman建立了联系,并给了我正确的答案。

我需要跟踪错误并知道为什么我没有建立连接。

我分享了到目前为止的代码,并附上了您发送给我的错误消息。

PHP代码(cURL)

<?php
    $xml = '<?xml version="1.0" encoding="utf-8"?>
    <methodCall>
     <methodName>listAccounts</methodName>
     <params>
      <param>
       <value>
        <struct>
         <member>
          <name>i_account</name>
          <value>
           <int>2763</int>
          </value>
         </member>
        </struct>
       </value>
      </param>
     </params>
    </methodCall>';

    $url = 'https://sip.serv.net.mx/xmlapi/xmlapi';

    $username = "ABACOM";
    $password = "PASSWORD";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
    curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);

    $result = curl_exec($ch);
    echo($result);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code

错误401

enter image description here

邮递员配置

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

看起来您的凭据被视为包含内容“ $ username:$ password”的字符串,而不是被解析。

尝试:

$credentials = "{$username}:{$password}";

curl_setopt($ch, CURLOPT_USERPWD, $credentials);

请注意缺少引号。