对 guzzle 的卷曲请求

curl request to guzzle

如何转换这个 curl 请求

卷曲请求

curl -L -F 'Request={"APIRequests":
[{"Verb":"UploadModelRelease","AuthToken":"","FirstName":"John","LastName":"Smith","Gender
":"0","AgeGroup":"1","Ethnics":"0","Country":"44"}]}' -F
'PropertyRelease=@Documents/jamie.pdf' https://api2.dreamstime.com/api/

处理 http 请求

guzzlecode


$client = new \GuzzleHttp\Client();

$response = $client->request('POST', "https://api2.dreamstime.com/api/",[
  'json'=>[
    'APIRequests' => [
            [
              "Verb" => "UploadRelease",
              "AuthToken" => $this->getAccessToken(),//token
              "ModelRelease" => $file,//file path
              "FirstName" => $firstName,
              "LastName" => $lastName,
              "Gender" => $gender,
              "Country" => $country,
              "AgeGroup" => $ageGroup,
              "Ethnics" => $ethnics,
            ],
        ],
  ]
]);

错误 “请上传发布文件”

我写了一个最接近 curl 请求的查询,因为 curl 请求有一个 -F 和文件,这意味着它正在发送文件,你将需要 multipart。由于我还没有测试过,所以我不能保证它会起作用

/**
 curl -L -F 'Request={"APIRequests":
[{"Verb":"UploadModelRelease","AuthToken":"","FirstName":"John","LastName":"Smith","Gender
":"0","AgeGroup":"1","Ethnics":"0","Country":"44"}]}' -F
'PropertyRelease=@Documents/jamie.pdf' https://api2.dreamstime.com/api/

*/
$client = new \GuzzleHttp\Client();

$response = $client->request('POST', "https://api2.dreamstime.com/api/",[
    'multipart' => [
        [
            'name'     => 'Request',
            'contents' => '{
                            "APIRequests":
                                [{"Verb":"UploadModelRelease","AuthToken":"","FirstName":"John","LastName":"Smith","Gender
                                ":"0","AgeGroup":"1","Ethnics":"0","Country":"44"}]
                            }' // place your json_encode here inplace of above
        ],
        [
            'name'     => 'PropertyRelease',
            'contents' => fopen("/path/to/file", "r")
        ]
    ]
]);