我使用的是旧的 php 版本,所以我无法在当前服务器上更新 guzzle。有办法吗set Content-Type :application/json?我在本地机器上使用的代码(我尝试了不同的方法来设置 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();我收到这样的回复: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我想得到这样的回应: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贡献1946条经验 获得超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 关注
- 186 浏览
添加回答
举报
0/150
提交
取消