php 如何将CURL命令转换为GuzzleHTTP

ktca8awb  于 2023-06-20  发布在  PHP
关注(0)|答案(1)|浏览(121)

我在想怎么把这个卷发变成狂饮

curl -XPOST -H 'Content-Type: application/json' -H 'x-client-id: 275432e3853bd165afbf5272' -H 'x-client-secret: 2279c0ffb9550ad0f9e0652741c8d06a49409517' -d '{
  "orderId": "Order0001",
  "orderAmount":1,
  "orderCurrency":"INR"
}' 'https://test.cashfree.com/api/v2/cftoken/order'

我试着在POSTMAN中导入这个curl请求,POSTMAN给了我php代码,最终复制粘贴到我的控制器上工作。

    • 工作控制器编码**
public function cashfreeToken(Request $request)
{
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://test.cashfree.com/api/v2/cftoken/order",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS =>"{\n  \"orderId\": \"Order0001\",\n  \"orderAmount\":1,\n  \"orderCurrency\":\"INR\"\n}",
      CURLOPT_HTTPHEADER => array(
        "Accept: application/json",
        "x-client-id: 275432e3853bd165afbf5272",
        "x-client-secret: 2279c0ffb9550ad0f9e0652741c8d06a49409517",
        "Content-Type: application/x-www-form-urlencoded"
      ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    dd($response);
}

但是我想知道如何在Guzzle中转换相同的请求。我尝试了,我得到400响应错误

    • 无法工作的Guzzle控制器代码**
**public function cashfreeToken(Request $request)
{
    // dd('working');
    $headers = [
        'Accept' => 'application/json',
        'x-client-id' => '275432e3853bd165afbf5272',
        'x-client-secret' => '2279c0ffb9550ad0f9e0652741c8d06a49409517'
    ];

    $GetOrder = [
            'orderId' => 'N10139',
            'orderAmount' => '123',
            'orderCurrency' => 'INR',
    ];

    $client = new \GuzzleHttp\Client();
    $request = $client->post('https://test.cashfree.com/api/v2/cftoken/order, ['headers' => $headers]);
    $request->setBody($GetOrder, 'application/json');

dd($request);
    // dd($response->getBody()->getContents());
}**
    • Api. php**
Route::post('cashfreetoken', 'Api\CashFreeController@cashfreeToken');
    • Postman **

    • 错误堆栈**
{"status":"ERROR","subCode":"400","message":"orderId is required"}
 in file /var/www/html/say-cure-web/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113

#0 /var/www/html/say-cure-web/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /var/www/html/say-cure-web/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /var/www/html/say-cure-web/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#3 /var/www/html/say-cure-web/vendor/guzzlehttp/promises/src/TaskQueue.php(47): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
#4 /var/www/html/say-cure-web/vendor/guzzlehttp/promises/src/Promise.php(246): GuzzleHttp\Promise\TaskQueue->run(true)
#5 /var/www/html/say-cure-web/vendor/guzzlehttp/promises/src/Promise.php(223): GuzzleHttp\Promise\Promise->invokeWaitFn()
#6 /var/www/html/say-cure-web/vendor/guzzlehttp/promises/src/Promise.php(267): GuzzleHttp\Promise\Promise->waitIfPending()
#7 /var/www/html/say-cure-web/vendor/guzzlehttp/promises/src/Promise.php(225): GuzzleHttp\Promise\Promise->invokeWaitList()
#8 /var/www/html/say-cure-web/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending()
#9 /var/www/html/say-cure-web/vendor/guzzlehttp/guzzle/src/Client.php(183): GuzzleHttp\Promise\Promise->wait()
#10 /var/www/html/say-cure-web/vendor/guzzlehttp/guzzle/src/Client.php(96): GuzzleHttp\Client->request('post', 'https://test.ca...', Array)
#11 /var/www/html/say-cure-web/app/Http/Controllers/Api/CashFreeController.php(30): GuzzleHttp\Client->__call('post', Array)
#12 [internal function]: App\Http\Controllers\Api\CashFreeController->cashfreeToken(Object(Illuminate\Http\Request))
#13 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#14 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('cashfreeToken', Array)
#15 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Api\CashFreeController), 'cashfreeToken')
#16 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\Routing\Route->runController()
#17 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(680): Illuminate\Routing\Route->run()
#18 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#19 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#21 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), 60, '1')
#23 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#24 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'api')
#25 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#27 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(657): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#28 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#29 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(612): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#30 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#31 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#32 /var/www/html/say-cure-web/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(65): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Barryvdh\Debugbar\Middleware\InjectDebugbar->handle(Object(Illuminate\Http\Request), Object(Closure))
#34 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#35 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#36 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#37 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#40 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#41 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#42 /var/www/html/say-cure-web/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#43 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#45 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#46 /var/www/html/say-cure-web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#47 /var/www/html/say-cure-web/public/index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))

POSTMAN生成的响应代码
任何关于这方面的帮助将不胜感激
使用版本^6.5为guzzlehttp/guzzle和Laravel版本6

ddrv8njm

ddrv8njm1#

$headers = [
    'Content-Type' => 'application/json',
    'X-Client-Id' => '275432e3853bd165afbf5272',
    'X-Client-Secret' => '2279c0ffb9550ad0f9e0652741c8d06a49409517',
];

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://test.cashfree.com/api/v2/cftoken/order', [
    'form_params' => [
        'orderId' => 'Order0001',
        'orderAmount' => 1,
        'orderCurrency' => 'INR'
    ],
    $headers,
]);

echo $response->getBody();

相关问题