我使用的是舊的 php 版本,所以我無法在當(dāng)前服務(wù)器上更新 guzzle。有辦法嗎set Content-Type :application/json?我在本地機(jī)器上使用的代碼(我嘗試了不同的方法來設(shè)置 Content-Type - 它被注釋掉了): $client = new \Guzzle\Http\Client(); $request = $client->post($cms_admin_api_url, ['headers' => [ 'user-agent' => $admin_token, //'Content-Type' => 'application/json', //'Accept' => 'application/json' ], //'Content-Type' => 'application/json' ], ['json' => [ 'name' => $key, 'image' => base64_encode($body), 'env' => $cake_emv ] ]); $response = $request->send();我收到這樣的回復(fù):POST /api/image/xxx HTTP/1.1Content-Length: 88Content-Type: application/x-www-form-urlencoded; charset=utf-8Headers: A7t1xXXXXXXXr23vt8jL999Host: my_api.url.comUser-Agent: Guzzle/3.9.3 curl/7.19.7 PHP/5.6.30X-Amzn-Trace-Id: Root=1-5ec66539-XXXXXXXXX9c3c667a3aX-Forwarded-For: 118.XXXX.242X-Forwarded-Port: 443X-Forwarded-Proto: httpsjson%5Bname%5D=article%2Fartem%2Ftest77.jpg&json%5Bimage%5D=eHh4&json%5Benv%5D=localhost我想得到這樣的回應(yīng):POST /api/image/xxx HTTP/1.1Content-Length: 70Content-Type: application/jsonHost: my_api.url.comUser-Agent: A7t1xXXXXXXXr23vt8jL999X-Amzn-Trace-Id: Root=1-5ec66539-XXXXXXXXX9c3c667a3aX-Forwarded-For: 118.XXXX.242X-Forwarded-Port: 443X-Forwarded-Proto: https{"name":"article\/artem\/test77.jpg","image":"eHh4","env":"localhost"}
1 回答

智慧大石
TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個贊
setBody()成功了:
$client = new \Guzzle\Http\Client();
$client->setUserAgent($admin_token);
$request = $client->post($cms_admin_api_url);
$request->setBody(json_encode([
'name' => $key,
'image' => base64_encode($data)
]), 'application/json');
$response = $request->send();
- 1 回答
- 0 關(guān)注
- 227 瀏覽
添加回答
舉報(bào)
0/150
提交
取消