dsn5510 2018-04-06 12:52
浏览 475
已采纳

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

I wrote this coden which is OK :

$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);

I want to convert this code into cURL command line. I've tried this :

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

but this cURL command line doesn't work, maybe something wrong link to parameter price[consumerPriceGross]... Any idea ?

  • 写回答

1条回答 默认 最新

  • douzhang8840 2018-04-06 16:14
    关注

    They API is expecting data in JSON format but that is doing a traditional application/x-www-form-urlencoded POST request.

    To get it to work, build a JSON string using some other utility and use that as for the POST data. To prevent curl from encoding the data, pass the -H "Content-type: application/json" header option.

    For example:

    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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog