【问题标题】:curl request to guzzle卷曲请求狂饮
【发布时间】:2021-08-20 17:37:03
【问题描述】:

如何转换这个 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/

guzzlehttp 请求

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,
            ],
        ],
  ]
]);

错误 "请上传发布文件"

【问题讨论】:

  • 嗨。你试过什么?
  • @AlexandreElshobokshy 我添加了我的代码
  • 请只分享相关代码,我们不需要全部查看
  • @AlexandreElshobokshy 这么好吗?
  • guzzle 代码与您的 curl 不匹配

标签: php curl guzzle


【解决方案1】:

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

/**
 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")
        ]
    ]
]);

【讨论】:

    猜你喜欢
    • 2018-09-20
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2021-05-31
    • 2011-04-23
    相关资源
    最近更新 更多