使用php和curl连接到api,用户名,密码和cookie

时间:2017-02-10 12:41:37

标签: php json api curl cookies

我在向api发出请求时收到错误:

  

HTTP / 1.1 401未经授权的服务器:nginx / 1。9。3日期:2017年2月10日星期五12:04:35 GMT内容长度:0连接:keep-alive WWW-Authenticate:BASIC realm =" application& #34; X-Powered-By:Undertow 1 Set-Cookie:rememberMe = deleteMe;路径= / ecocashGateway-preprod;最大年龄= 0; Expires = Thu,09-Feb-2017 12:04:35 GMT Set-Cookie:PAYMENTS_CLUSTER = PAY_02;路径= /

这是我的php和curl请求:

$url = 'https://payonline.econet.co.zw/ecocashGateway-preprod/payment/v1/transactions/amount'; 
//Initiate cURL. 
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-length:'.strlen($jsonDataEncoded))
);

0 个答案:

没有答案