【问题标题】:How can I create the curl request exactly as a guzzle?我怎样才能像 guzzle 一样创建 curl 请求?
【发布时间】:2022-11-04 19:09:31
【问题描述】:

我有一个如下所示的 curl 请求,我正在尝试将其转换为 guzzle,但是当我向 cloudflare 发送请求时,它一直向我返回一个错误。 “解码错误” 我的 guzzle 请求中是否有错误?正常的 curl 请求应该是稳定的。

`curl \
-X POST \
-d '{"url":"https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4","meta":{"name":"My First Stream Video"}}' \
-H "Authorization: Bearer <API_TOKEN>" \
https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/stream/copy`

我的代码在下面可用。

 $response = $client->request('POST', 'https://api.cloudflare.com/client/v4/accounts/' . $accountId . '/stream/copy', [
                    'headers' => [
                        'Authorization' => 'Bearer ' . $token,
                    ],

    'form_params' => [
        "url" => "https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4",
        "meta" => [
            "name": "My First Stream Video"
        ]
    ]

【问题讨论】:

    标签: laravel


    【解决方案1】:

    尝试设置内容类型标题。

    $response = $client->request('POST', 'https://api.cloudflare.com/client/v4/accounts/' . $accountId . '/stream/copy', [
                        'headers' => [
                            'Content-Type' => 'application/json',
                            'Authorization' => 'Bearer ' . $token,
                        ],
    
        'form_params' => [
            "url" => "https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4",
            "meta" => [
                "name": "My First Stream Video"
            ]
        ]
    

    【讨论】:

    • 我试试这个。结果prnt.sc/sVMvYIN0rmaY 我不明白为什么cloudflare 有这么大的问题。
    【解决方案2】:

    这个答案是使用 Laravel 的 HTTP 请求。

    Http::withBody('{"url":"https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4","meta":{"name":"My First Stream Video"}}')
        ->withToken('<API_TOKEN>')
        ->post('https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/stream/copy');
    

    另外,请查看 Shift 拥有的这个神奇工具

    https://laravelshift.com/convert-curl-to-http

    【讨论】:

    • curl 转换器是一个很酷的工具,Jason McCreary 非常出色地构建了它。
    • 谢谢,但我使用的是 laravel 5.5 :(
    【解决方案3】:

    这就是我找到解决方案的方式。如果有人有问题,他们可以使用它。

    'headers' => [
        'Authorization' => 'Bearer ' . $token,
        'Content-Type' => 'application/json',
    ],
    'json' => [
        "url" => "https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4",
        "meta" => [
            "name": "My First Stream Video"
        ]
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 2013-09-16
      • 2020-01-17
      相关资源
      最近更新 更多