我想在curl_setopt($ch, CURLOPT_POSTFIELDS)
函数中发送一个对象(javascript对象(,我试了一些方法,但找不到合适的组合。我尝试了下面的代码,但在这一行中得到了以下错误syntex error unexpected {
:curl_setopt($ch, CURLOPT_POSTFIELDS, {
我应该如何写对象?在PHP 中最好的做法是什么
这是目标示例:
{
'Key': '',
'Local': 'us',
'UniqueId': '',
'SuccessUrl': '',
'CancelUrl': '',
'CallbackUrl': '',
'PaymentType': 'regular',
'CreateInvoice': 'false',
'AdditionalText': '',
'ShowCart': 'true',
'Installments': {
Type: 'regular' ,
MinQuantity: '1',
MaxQuantity: '12'
},
'Customer': {
'Email': 'someone@gmail.com',
'Name': 'Demo Client' ,
'PhoneNumber': '031351315',
'Attributes': {
'HolderId': 'none' ,
'Name': 'required' ,
'PhoneNumber': 'required' ,
'Email': 'optional'
}
},
'CartItems': [{
'Amount': '10.20',
'Currency': 'usd',
'Name': 'My Item1 Name',
'Description': 'My Item description , comes below the name' ,
'Quantity': 2 ,
'Image': 'https://google.com' ,
'IsTaxFree': 'false'
}]
};
这是我尝试过的:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://exampleurl.com/CreateSession");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, {
"Key": "1231231231231321321321321",
"Local": "us",
"UniqueId": "",
"SuccessUrl": "",
"CancelUrl": "",
"CallbackUrl": "",
"PaymentType": "regular",
"CreateInvoice": "false",
"AdditionalText": "",
"ShowCart": "true",
"Installments": {
Type: "regular" ,
MinQuantity: "1",
MaxQuantity: "12"
},
"Customer": {
"Email": "someone@gmail.com",
"Name": "Demo Client" ,
"PhoneNumber": "21351135",
"Attributes": {
"HolderId": "none" ,
"Name": "required" ,
"PhoneNumber": "required" ,
"Email": "optional"
}
},
"CartItems": [{
"Amount": "10.20",
"Currency": "usd",
"Name": "My Item1 Name",
"Description": "My Item description , comes below the name" ,
"Quantity": 2 ,
"Image": "https://google.com.png" ,
"IsTaxFree": "false"
}]
});
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json; charset=utf-8"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
这是一个字符串,所以应该用引号括起来:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
"Key": "1231231231231321321321321",
"Local": "us",
"UniqueId": "",
"SuccessUrl": "",
"CancelUrl": "",
"CallbackUrl": "",
"PaymentType": "regular",
"CreateInvoice": "false",
"AdditionalText": "",
"ShowCart": "true",
"Installments": {
Type: "regular" ,
MinQuantity: "1",
MaxQuantity: "12"
},
"Customer": {
"Email": "someone@gmail.com",
"Name": "Demo Client" ,
"PhoneNumber": "21351135",
"Attributes": {
"HolderId": "none" ,
"Name": "required" ,
"PhoneNumber": "required" ,
"Email": "optional"
}
},
"CartItems": [{
"Amount": "10.20",
"Currency": "usd",
"Name": "My Item1 Name",
"Description": "My Item description , comes below the name" ,
"Quantity": 2 ,
"Image": "https://google.com.png" ,
"IsTaxFree": "false"
}]
}");
将JSON数据存储在一个变量中,然后简单地发布。
<?php
$ch = curl_init();
$jsonPost = `{
"Key": "1231231231231321321321321",
"Local": "us",
"UniqueId": "",
"SuccessUrl": "",
"CancelUrl": "",
"CallbackUrl": "",
"PaymentType": "regular",
"CreateInvoice": "false",
"AdditionalText": "",
"ShowCart": "true",
"Installments": {
Type: "regular" ,
MinQuantity: "1",
MaxQuantity: "12"
},
"Customer": {
"Email": "someone@gmail.com",
"Name": "Demo Client" ,
"PhoneNumber": "21351135",
"Attributes": {
"HolderId": "none" ,
"Name": "required" ,
"PhoneNumber": "required" ,
"Email": "optional"
}
},
"CartItems": [{
"Amount": "10.20",
"Currency": "usd",
"Name": "My Item1 Name",
"Description": "My Item description , comes below the name" ,
"Quantity": 2 ,
"Image": "https://google.com.png" ,
"IsTaxFree": "false"
}]
}`;
curl_setopt($ch, CURLOPT_URL, "https://exampleurl.com/CreateSession");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPost);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json; charset=utf-8"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
syntex意外错误{
因为您缺少" "
或' '
,
试试这个
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
'Key': '',
'Local': 'us',
'UniqueId': '',
'SuccessUrl': '',
'CancelUrl': '',
'CallbackUrl': '',
'PaymentType': 'regular',
'CreateInvoice': 'false',
'AdditionalText': '',
'ShowCart': 'true',
'Installments': {
Type: 'regular' ,
MinQuantity: '1',
MaxQuantity: '12'
},
'Customer': {
'Email': 'someone@gmail.com',
'Name': 'Demo Client' ,
'PhoneNumber': '031351315',
'Attributes': {
'HolderId': 'none' ,
'Name': 'required' ,
'PhoneNumber': 'required' ,
'Email': 'optional'
}
},
'CartItems': [{
'Amount': '10.20',
'Currency': 'usd',
'Name': 'My Item1 Name',
'Description': 'My Item description , comes below the name' ,
'Quantity': 2 ,
'Image': 'https://google.com' ,
'IsTaxFree': 'false'
}]
};");
我想解释一下,但是我的英语不好,对不起。