我正在使用multipart / form-data连接到API,如果我在命令行上通过curl发送请求我没有问题,但是当使用PHP cURL发送它时,我收到内部服务器500错误。
命令行是
curl -H "Content-Type: multipart/form-data" -H "X-DevKey: XXX" -H "X-AccessToken: XXX" -F "data=@file.json; type=application/json" -X POST https://api.site.com/v1/Items -i -v
其中,给我以下回复
> POST /v1/Items HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5
> Host: api.site.com
> Accept: */*
> X-DevKey: XXX
> X-AccessToken: XXX
> Content-Length: 1008
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=----------------------------58bf5d52327e
>
< HTTP/1.1 100 Continue
HTTP/1.1 100 Continue
< Set-Cookie: sto-id=XXX; Expires=Fri, 16-May-2025 15:32:27 GMT; Path=/
Set-Cookie: sto-id=XXX; Expires=Fri, 16-May-2025 15:32:27 GMT; Path=/
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Cache-Control: no-cache
Cache-Control: no-cache
< Pragma: no-cache
Pragma: no-cache
< Content-Type: application/json; charset=utf-8
Content-Type: application/json; charset=utf-8
< Expires: -1
Expires: -1
< X-Powered-By: ASP.NET
X-Powered-By: ASP.NET
< Date: Tue, 19 May 2015 15:32:28 GMT
Date: Tue, 19 May 2015 15:32:28 GMT
< Content-Length: 220
Content-Length: 220
< Set-Cookie: sto-id=XXX; Expires=Fri, 16-May-2025 15:32:28 GMT; Path=/
Set-Cookie: sto-id=XXX; Expires=Fri, 16-May-2025 15:32:28 GMT; Path=/
<
* Connection #0 to host api.site.com left intact
{"userMessage":"Item listed with itemID: 484495200","developerMessage":"Item listed with itemID: 484495200","links":[{"rel":"self","href":"https://api.site.com/v1/items/484495200","verb":"GET","title":"484495200"}]}* Closing connection #0
## Heading ##* SSLv3, TLS alert, Client hello (1):
我当前的PHP函数看起来像这样
function create_listing($buynow, $startingBid, $category, $description, $picture, $sku, $title)
{
$url = "https://api.site.com/v1/Items";
$token = get_access_token();
$headers = array(
"X-DevKey: XXX",
"X-AccessToken: $token",
"Expect: 100-continue",
"Content-Type: multipart/form-data; Boundary=----WebKitFormBoundaryR7f7XrG1vJhOfHzu"
);
$data = array(
"AutoRelist" => 1,
"AutoRelistFixedCount" => 0,
"BuyNowPrice" => 9.99,
"CategoryID" => 2325,
"Condition" => 1,
"CountryCode" => 'US',
"Description" => $description,
"InspectionPeriod" => 1,
//"FixedPrice" => $fixedprice,
"IsFFLRequired" => true,
"ListingDuration" => 1,
"PaymentMethods" => array(
"Check" => false,
"VisaMastercard" => true,
"COD" => false,
"Escrow" => false,
"Amex" => false,
"PayPal" => false,
"Discover" => true,
"SeeItemDesc" => true,
"CertifiedCheck" => true,
"USPSMoneyOrder" => true,
"MoneyOrder" => false
),
"PostalCode" => '85388',
"Quantity" => 1,
//"ReservePrice" => $reserve,
"SalesTaxes" => array(
array(
"State" => 'AZ',
"TaxRate" => 8.5
)
),
//"SerialNumber" => $serial,
"ShippingClassCosts" => array(
"Ground" => 25.00,
"Priority" => 25.00
),
"ShippingClassesSupported" => array(
"Overnight" => false,
"TwoDay" => false,
"ThreeDay" => false,
"Ground" => true,
"FirstClass" => false,
"Priority" => true,
"Other" => false
),
//"ShippingProfileID" => $shippingProfile,
"SKU" => $sku,
"StartingBid" => 0.99,
"Title" => $title,
//"UPC" => $upc,
//"Weight" => $weight,
//"WeightUnit" => 1,
"WhoPaysForShipping" => 8,
//"ItemPremiumFeatures" => array(
//
//),
"WillShipInternational" => false
);
$boundary = '----WebKitFormBoundaryR7f7XrG1vJhOfHzu';
$request = "$boundary\nContent-Disposition: form-data; name=\"data\"\n";
$request .= json_encode($data) . "\n$boundary--";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$verbose = fopen('php://temp', 'rw+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
$result = curl_exec($ch);
if ($result === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
htmlspecialchars(curl_error($ch)));
}
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
$info = curl_getinfo($ch);
if (!$result) {
echo curl_error($ch);
}
curl_close($ch);
return $result;
}
但我收到以下回复:
> POST /v1/Items HTTP/1.1
Host: api.site.com
Accept: */*
Cookie: sto-id=JKHHNMED
X-DevKey: XXX
X-AccessToken: XXX
Expect: 100-continue
Content-Type: multipart/form-data; Boundary=----WebKitFormBoundaryR7f7XrG1vJhOfHzu
Content-Length: 939
< HTTP/1.1 100 Continue
< HTTP/1.1 500 Internal Server Error
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: application/json; charset=utf-8
< Expires: -1
< X-Powered-By: ASP.NET
< Date: Tue, 19 May 2015 17:11:40 GMT
< Content-Length: 219
* HTTP error before end of send, stop sending
任何有关我为什么会收到500错误的帮助都将不胜感激。