使用数组参数将POST cURL PHP请求转换为cURL命令行

时间:2018-04-06 12:52:52

标签: php curl post

我写了这个代码,这是好的:

$veh['vehicleClass']        = 'Car';
$veh['category']            = 'Van';
$veh['make']                = 'RENAULT';
$veh['model']               = 'Scenic';
$veh['modelDescription']    = 'Turbopoooower';
$veh['firstRegistration']   = "201606";
$veh['mileage']             = "500";
$veh['damageUnrepaired']    = true;
$veh['condition']           = "USED";
$veh['internalNumber']      = "12";
$veh['price']['consumerPriceGross'] = "5400";

$ch = curl_init();

$proxy = PROXY_MOBILE_DE;
$proxy_port = PROXY_PORT_MOBILE_DE;
$loginpassw = LOGINPASSWD_MOBILE_DE;

$url='https://services.mobile.de/seller-api/sellers/1086/ads';

$headers = array();
$headers[] = "Content-Type: application/vnd.de.mobile.api+json";

curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $loginpassw);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($vehicle));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$data = curl_exec($ch);

curl_close($ch);

我想将此代码转换为cURL命令行。 我试过这个:

curl -k -x api.test.sandbox.mobile.de:8080  -basic -u XXX:YYY -d "vehicleClass=Car" -d "category=Van" -d "make=RENAULT" -d "model=Scenic" -d "modelDescription=Turbopoooower" -d "condition=USED" -d "damageUnrepaired=true" -d "firstRegistration=201606" -d "internalNumber=13" -d "mileage=500" -d "price[consumerPriceGross]=5400" -X POST "https://services.mobile.de/seller-api/sellers/1086/ads" -H "Accept: application/vnd.de.mobile.api+json

但是这个cURL命令行不起作用,可能是指向参数价格[consumerPriceGross]的错误链接... 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

他们的API期待JSON格式的数据,但这是传统的application/x-www-form-urlencoded POST请求。

要使其工作,请使用其他实用程序构建JSON字符串,并将其用作POST数据。要防止curl对数据进行编码,请传递-H "Content-type: application/json"标题选项。

例如:

curl -k -x api.test.sandbox.mobile.de:8080  \
-basic -u XXX:YYY \
-H "Accept: application/vnd.de.mobile.api+json" \
-H "Content-type: application/json" \
-d '{"vehicleClass":"Car","category":"Van","make":"RENAULT","model":"Scenic","modelDescription":"Turbopoooower","firstRegistration":"201606","mileage":"500","damageUnrepaired":true,"condition":"USED","internalNumber":"12","price":{"consumerPriceGross":"5400"}}'
"https://services.mobile.de/seller-api/sellers/1086/ads"