我必须向API发送请求。请求应包含一些数据以及4个头参数。我要发送的原始数据是$ raw_data。
错误是:内部服务器错误
我的代码如下:
<?php
function CurlSendPostRequest($url,$request="")
{
$raw_data='{
"job_id": null,
"collectionOnDelivery": false,
"invoice": null,
"collectionDate": "2015-01-30T09:00:00",
"consolidate": false,
"consignment": [{
"consignmentNumber": null,
"consignmentRef": null,
"parcels": [],
"collectionDetails": {
"contactDetails": {
"contactName": "My Contact",
"telephone": "0121 500 2500"
},
"address": {
"organisation": "GeoPostUK Ltd",
"countryCode": "GB",
"postcode": "B66 1BY",
"street": "Roebuck Lane",
"locality": "Smethwick",
"town": "Birmingham",
"county": "West Midlands"
}
},
"deliveryDetails": {
"contactDetails": {
"contactName": "My Contact",
"telephone": "0121 500 2500"
},
"address": {
"organisation": "GeoPostUK Ltd",
"countryCode": "GB",
"postcode": "B66 1BY",
"street": "Roebuck Lane",
"locality": "Smethwick",
"town": "Birmingham",
"county": "West Midlands"
},
"notificationDetails": {
"email": "my.email@geopostuk.com",
"mobile": "07921000001"
}
},
"networkCode": "2^12",
"numberOfParcels": 1,
"totalWeight": 5,
"shippingRef1": "My Ref 1",
"shippingRef2": "My Ref 2",
"shippingRef3": "My Ref 3",
"customsValue": null,
"deliveryInstructions": "Please deliver with neighbour",
"parcelDescription": "",
"liabilityValue": null,
"liability": false
}]
}';
// $authentication = base64_encode("USER:PASS");
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_POSTFIELDS => $raw_data,
// CURLOPT_FOLLOWLOCATION => false, // follow redirects
// CURLOPT_ENCODING => "utf-8", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
// CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect
// CURLOPT_TIMEOUT => 20, // timeout on response
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $request, // this are my post vars
// CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
// CURLOPT_SSL_VERIFYPEER => false, //
// CURLOPT_VERBOSE => 1,
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Content-Type: application/json",
"GEOClient: account/XX-ACC_NO-XX",
"GEOSession: XX-SESS_ID-XX"
)
);
curl_setopt_array($ch,$options);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
//echo $curl_errno;
//echo $curl_error;
print_r($data);
curl_close($ch);
return $data;
}
$url = "https://api.URL.com";
CurlSendPostRequest($url);
?>
答案 0 :(得分:0)
犯了一个非常愚蠢的错误:我添加了一行
CURLOPT_POSTFIELDS => $raw_data,
我添加了以下几行
CURLOPT_POSTFIELDS => $request,
我的原始数据的值在$ raw_data中,因此$ request覆盖了那个。只需要评论该行:
CURLOPT_POSTFIELDS => $request,
它很棒!