将 CURL_OPTPOST 设置为 true 时,CURL http 身份验证失败


CURL http authentication fails when setting CURL_OPTPOST to true

>我正在尝试构建一个与Firstdata的api集成的付款表单。我需要将XML字符串发布到他们的服务器。它们还需要客户端证书和 http 身份验证。我的 CURL 设置当前如下所示:

function firstdata_send($config_param, $data) {
  $config_default = array(
    'test' => FALSE,
  );
  // settings in $config_param will overwrite settings in $config_default
  $config = (object)array_merge($config_default, $config_param);
  if($config->test) {
    $url = 'https://ws.merchanttest.firstdataglobalgateway.com/fdggwsapi/services/order.wsdl';
  }
  else {
    $url = 'https://ws.firstdataglobalgateway.com/fdggwsapi/services/order.wsdl';
  }
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, "{$config->username}:{$config->password}");
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSLCERT, $config->pemfile);
  curl_setopt($ch, CURLOPT_SSLKEY, $config->keyfile);
  curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $config->keypass);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, TRUE);
  curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
  $result = curl_exec($ch);
  $result .= curl_error($ch);
  return $result;
}

他们的服务器以 HTTP/1.1 401 Unauthorized 响应。但是,如果我注释掉帖子选项:

  //curl_setopt($ch, CURLOPT_POST, TRUE);
  //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

我得到HTTP/1.1 200 OK.除非我完全误解了发生了什么,否则使用 post 似乎以某种方式干扰了身份验证标头。我不知道我错过了什么。

解决:

事实证明,测试帐户生成的 SSL 证书是错误的。我不得不打电话给他们的技术支持,他们必须重新生成证书 3 次,系统才会接受它们。很抱歉浪费你的时间。我应该先给他们打电话。如果有人感兴趣,我拨打的技术支持电话是(888)477-3611。我认为NomikOS最接近正确,所以我会把他标记为答案,并投票给你们其他人。再次感谢。

形成我所看到的,您需要一个pem格式的certificate才能使用此服务....

A. 下载 API https://www.firstdata.com/downloads/customerservice/30006_api_php.zip

二.你会看到很多例子

销售信息示例

    include"lphp.php";
    $mylphp=new lphp;
    $myorder["host"]       = "secure.linkpt.net";
    $myorder["port"]       = "1129";
    $myorder["keyfile"]    = "./YOURCERT.pem"; # Change this to the name and location of your certificate file 
    $myorder["configfile"] = "1234567";        # Change this to your store number 
    $myorder["ordertype"]    = "SALE";
    $myorder["result"]       = "LIVE"; # For a test, set result to GOOD, DECLINE, or DUPLICATE
    $myorder["cardnumber"]   = "4111-1111-1111-1111";
    $myorder["cardexpmonth"] = "01";
    $myorder["cardexpyear"]  = "05";
    $myorder["chargetotal"]  = "9.99";
    $myorder["addrnum"]   = "123";   # Required for AVS. If not provided, transactions will downgrade.
    $myorder["zip"]       = "12345"; # Required for AVS. If not provided, transactions will downgrade.
//  $myorder["debugging"] = "true";  # for development only - not intended for production use

  # Send transaction. Use one of two possible methods  #
//  $result = $mylphp->process($myorder);       # use shared library model
    $result = $mylphp->curl_process($myorder);  # use curl methods

    if ($result["r_approved"] != "APPROVED")    // transaction failed, print the reason
    {   
        print "Status: $result[r_approved]'n";
        print "Error: $result[r_error]'n";
    }
    else
    {   // success
        print "Status: $result[r_approved]'n";
        print "Code: $result[r_code]'n";
        print "OID: $result[r_ordernum]'n'n";
    }

检查您的访问凭据(用户名、密码)

401 未经授权

该请求需要用户身份验证。响应必须包括 包含质询的 WWW-身份验证标头字段(第 14.47 节) 适用于请求的资源。客户端可以重复 具有合适的授权标头字段的请求(第 14.8 节)。如果 请求已包含授权凭据,然后是 401 答复表明,已拒绝对这些人员进行授权 凭据。如果 401 响应包含与 先前的响应,并且用户代理已尝试 身份验证至少一次,然后应该向用户呈现 响应中给出的实体,因为该实体可能包含 相关诊断信息。HTTP 访问身份验证是 在"HTTP 身份验证:基本访问和摘要式访问"中进行了说明 身份验证"[43]。

来源: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2

==

OBS:我推荐你这段代码来检查错误

// check for errors before close
$result = curl_exec($ch);
if ($result === false)
{
    echo curl_error($ch);
}
curl_close($ch);

刚刚检查过 First Datas API 是一个基于 SOAP 的 API...

wsdl 的 url 只接受 GET,因为 wsdl 只是用于发送 SOAP 调用的 xml 指令集。

PHP soapClient 类