簡體   English   中英

使用數組參數將POST cURL PHP請求轉換為cURL命令行

[英]convert POST cURL PHP request to cURL command line using array parameters

我寫了這個coden,還可以:

$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命令行不起作用,可能是指向參數price [consumerPriceGross]的錯誤鏈接...知道嗎?

他們的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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM