将cURL转换为GuzzlePost发送json数据



我有这个cURL命令,需要使用Guzzle 7将其转换为PHP。我已经研究了好几天了,但进展甚微。cURL命令使用Simpli.fi api在父组织下创建一个组织。

curl -i -X POST -H "X-App-Key: xxxx-xx-xx-xx-xxxxxx" -H "X-User-Key: xxxx-xx-xx-xx-xxxxxx" 
-H "Content-Type: application/json" 
-d '{
"organization": {
"name": "My New Organization",
"parent_id": "5786",
"custom_id": "<Put your organization identifier here or omit this optional field>"
}
}' 
"https://app.simpli.fi/api/organizations"

我可以使用这个网站转换它,但它不使用Guzzle:https://onlinedevtools.in/curl这是它给我的:

include('vendor/rmccue/requests/library/Requests.php');
Requests::register_autoloader();
$headers = array(
'X-App-Key' => 'xxxx-xx-xx-xx-xxxxxx',
'X-User-Key' => 'xxxx-xx-xx-xx-xxxxxx',
'Content-Type' => 'application/json'
);
$data = '{n        "organization": {n          "name": "My New Organization",n          "parent_id": "5786",n          "custom_id": "<Put your organization identifier here or omit this optional field>"n        }n      }';
$response = Requests::post('https://app.simpli.fi/api/organizations', $headers, $data);

以下是我迄今为止除了上面转换的代码之外所做的尝试:

public static function createOrganization()
{
self::init();
$client = new Client(['debug' => true]);
$request = $client->request('POST',
self::$baseUrl.'/organizations', [
'multipart' => [
[
'name' => 'data',
'contents' => "{'organization':{'name':'Pete's Pet Pagoda','parent_id':'1052385'}}",
],
],
'headers' => [
'x-app-key'   => self::$appKey,
'x-user-key'  => self::$userKey,
'Content-Type' => 'application/json',
]
]);
dd($response = $request->getStatusCode());
}

我得到了很多不同的错误,但这是最新的:

curl_setopt_array((:不能将Output类型的流表示为STDIO FILE*

有人能告诉我我做错了什么吗?有什么东西不见了吗?

更新:经过对这个问题的进一步研究,并在Laracast Slack频道上与一位开发人员聊天,我了解到这是在Windows系统上运行时['debug' => true]选项的一个错误,在这个GITHUB页面上进行了描述:https://github.com/guzzle/guzzle/issues/1413我在Windows 10 Pro系统上运行。我通过将调试选项更改为使用fopen((来纠正它,如下所示:


'debug' => fopen('php://stderr', 'w'),

我使用PHPStorm。它建议使用"wb"使其二进制安全。更改后,帖子请求运行良好。

您需要使用body,而不是multipart。您也可以使用json

$request = $client->request('POST',
self::$baseUrl.'/organizations', [
'headers' => [
'x-app-key'   => self::$appKey,
'x-user-key'  => self::$userKey,
'Content-Type' => 'application/json',
],
'body' => [
'{"organization":
[
{
"name":"Pete`s Pet Pagoda",
"parent_id":"1052385"

}
]
}',
],
]);

方法2

您可以将数组传递给json请求选项,它会在发送guzzle请求时将其转换为json。不需要使用头作为application/json,因为它在内部应用。

$client->request('POST',
self::$baseUrl.'/organizations', [
'headers' => [
'x-app-key'   => self::$appKey,
'x-user-key'  => self::$userKey,
'Content-Type' => 'application/json',
],
'json' => [
"organization" => [
[
"name" => "Pete`s Pet Pagoda"
"parent_id" => "1052385"
]
]
],
]);

我希望这将对您有所帮助。要进行进一步的调试,请使用Middleware::tap(在这里找到更多帮助中间件+json+请求(

try{
$client = new Client();
$response = $client->request('POST', self::$baseUrl.'/organizations', 
[
'headers' => [
'x-app-key'   => self::$appKey,
'x-user-key'  => self::$userKey,
'Content-Type' => 'application/json',
],
'json' => [
'organization' => 
[
"name" => "some_name_value",
"parent_id" => "id_here",
"custom_id" => "custom id here"
]
]
]);
$_response = json_decode($response->getBody()->getContents(), true);
}
catch(BadResponseException $e){
$response = $e->getResponse();
$errorArray = json_decode($response->getBody()->getContents(), true);

//echo "<pre>";
//print_r($errorArray);
//return some message from errorarray;
}

最新更新