Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将内容类型更正为json_Php_Web Services_Curl - Fatal编程技术网

Php 将内容类型更正为json

Php 将内容类型更正为json,php,web-services,curl,Php,Web Services,Curl,我正在使用web服务,我必须将json发布到他们的web服务中,例如: $json = ' { "RequestHeader": { "SpecVersion": "1.2", "CustomerId": "120", "RequestId": "111", "RetryIndicator": 0 }, "TerminalId": "17800144", "Payment": { "Amount": { "Value": "10

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

$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"
  }
}
';
对于这个Web服务,我必须是post
Content-Type
as
application/json;charset=utf-8
带有
cUrl
,但我得到以下错误结果:

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;}
选中此项:“$headers”数组将导致错误消息<代码>“授权:基本用户\u通行证”错误:身份验证\u失败-无法验证请求-授权参数的格式错误