php - cUrl 将正确的内容类型发布为 json

标签 php web-services curl

我正在使用 saferpay网络服务,我必须将 json 发布到他们的网络服务,例如:

$json = '
{
  "RequestHeader": {
    "SpecVersion": "1.2",
    "CustomerId": "120",
    "RequestId": "111",
    "RetryIndicator": 0
  },
  "TerminalId": "17800144",
  "Payment": {
    "Amount": {
      "Value": "100",
      "CurrencyCode": "CHF"
    },
    "OrderId": "1",
    "Description": "Description of payment"
  },
  "ReturnUrls": {
    "Success": "http://google.com",
    "Fail": "http://google.com/1"
  }
}
';

对于此网络服务,我必须将 Content Type 发布为 application/json; charset=utf-8cUrl,但我得到这个错误结果:

Array
(
    [ErrorName] => VALIDATION_FAILED
    [ErrorMessage] => Content type not supported
    [Behavior] => ABORT
    [ErrorDetail] => Array
        (
            [0] => Content type 'application/x-www-form-urlencoded' is Not supported. Please use 'application/json; charset=utf-8'.
        )
)

在我的代码中我发布了正确的内容类型,但我不知道有什么问题:

print_r(do_post("https://test.saferpay.com/api/Payment/v1/Transaction/Initialize",$json));
function do_post($url, $JSONObj){
    //Set Options for CURL
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    //Return Response to Application
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //Set Content-Headers to JSON
    curl_setopt($curl, CURLOPT_HTTPHEADER,array("Content-Type: application/json; charset=utf-8"));
    curl_setopt($curl, CURLOPT_HTTPHEADER,array("Authorization: Basic USER_PASS"));
    //Execute call via http-POST
    curl_setopt($curl, CURLOPT_POST, true);
    //Set POST-Body
        //convert DATA-Array into a JSON-Object
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($JSONObj));
    //WARNING!!!!!
    //This option should NOT be "false"
    //Otherwise the connection is not secured
    //You can turn it of if you're working on the test-system with no vital data
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    $jsonResponse = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ( $status != 200 ) {
        //die($status);
    }
    curl_close($curl);
    $response = json_decode($jsonResponse, true);
    return $response;
} 

最佳答案

尝试,

这段代码在 curl 函数中会起作用

function do_post($url, $JSONObj){
$headers = array( "Content-Type: application/json; charset=utf-8" ,"Authorization: Basic USER_PASS" );
//Set Options for CURL
$curl = curl_init($url);
//Return Response to Application
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Set Content-Headers to JSON
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
//Execute call via http-POST
curl_setopt($curl, CURLOPT_POST, true);
//Set POST-Body
    //convert DATA-Array into a JSON-Object
curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONObj);
//WARNING!!!!!
//This option should NOT be "false"
//Otherwise the connection is not secured
//You can turn it of if you're working on the test-system with no vital data
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$jsonResponse = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
    //die($status);
}
curl_close($curl);
$response = json_decode($jsonResponse, true);
return $response;}

关于php - cUrl 将正确的内容类型发布为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36565841/

相关文章:

php - Laravel SOLID 使用存储库模式

PHP Laravel 如何使用两个表进行排序

php - 如何限制我网站的 API 用户?

javascript - 将 json 字符串值的一部分传递给 jquery 函数

web-services - 动态 GP Web 服务 : Limited?

PHP cURL POST 请求无法发送?

java - 从网站提取数据的方法?

java - RESTful Web 应用程序是什么意思?

php - 如果使用 cURL 或 Wget 访问页面,则在 PHP 中检测

PHP CURL CURLOPT_SSL_VERIFYPEER 被忽略