Api JSON解码回复 [英] Api JSON Decode Reply

查看:126
本文介绍了Api JSON解码回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im尝试按照以下命令接收来自api的回复

im trying to receive an reply from the api as per below command

$client=new \GuzzleHttp\Client();
                $response=$client->request(
                    'post',
                    'https://cowpay.me/api/v0/fawry/charge-request-cc',
                    [
                    'json' =>[
                        'merchant_code' => $merchant_code,
                        'merchant_reference_id' => $merchant_reference_id,
                        'signature' => hash('sha256',$merchant_code.$merchant_reference_id.$user_id.$payment_method.$item_amout_fix.$m_hash_key),
                        'customer_name'=>$user_name,
                        'customer_email'=>$customer_email,
                        'customer_mobile'=>  $customer_mobile,
                        'customer_merchant_profile_id' => $user->id,
                        'currency_code' =>'EGP',
                        'amount' => $item_amout_fix,
                        'payment_method'=>'CARD',
                        'description' => $description,
                        'charge_items'=> [
                            'itemId'=> $item_number,
                            'description'=> $description,
                            'price'=> $item_amout_fix,
                            'quantity'=> '1',
                            ],
                            'card_number' => $request->card,
                            'expiry_year' => $request->year,
                            'expiry_month' => $request->month,
                            'cvv' => $request->cvv,
                            'save_card'     => '0',
                            ]
                            ] );
$response=json_decode($response->getBody()->getContents(), true);
return $response;

它曾经如何工作并以以下形式获得响应 https://prnt.sc/s4xcra

how ever this used to work and get the response in the form of https://prnt.sc/s4xcra

但是现在我收到服务器错误,并且laravel日志以清晰,正确的状态代码清楚地显示了该消息,但不会将其返回到前端

but now i get server error and laravel logs shows the message clearly with a clear and correct status code but wont get it back to the front end

local.ERROR: Client error: `POST https://cowpay.me/api/v0/fawry/charge-request-cc` resulted in a `422 Unprocessable Entity` response:
{"success":false,"message":"The given data was invalid.","status_code":422,"status_description":"The given data was inva (truncated...)

需要注意的是,该代码是完全正确的,因为我发送了一个有效的数据,并且为我提供了另一个状态代码,但是在laravel> storage> logs>

to be noted the code is totally correct as i send a valid data and it gives me another status code but in laravel> storage > logs >

推荐答案

检查

Check the CowPay API docs. The 422 status code is a response from CowPay telling you that the data you're sending them is invalid.

猜测是因为您没有正确设置请求的标头和正文.您的JSON应该在请求的主体中,并使用json_encode进行编码.您的标头还应列出您的请求的Content-Type.

At a guess, it's because you haven't correctly set the headers and body of your request. Your JSON should be in the body of the request, and encoded as such with json_encode. Your headers should also list the Content-Type of your request.

尝试将您的请求参数更改为以下内容:

Try changing the arguments of your request to the following:

 'post',
 'https://cowpay.me/api/v0/fawry/charge-request-cc',
 [
     "headers" => [
         "Accept" => "application/json",
         "Content-Type" => "application/json"
     ],
     "body" => json_encode([
         'merchant_code' => $merchant_code,
         'merchant_reference_id' => $merchant_reference_id,
         'signature' => hash('sha256',$merchant_code.$merchant_reference_id.$user_id.$payment_method.$item_amout_fix.$m_hash_key),
         'customer_name'=>$user_name,
         'customer_email'=>$customer_email,
         'customer_mobile'=>  $customer_mobile,
         'customer_merchant_profile_id' => $user->id,
         'currency_code' =>'EGP',
         'amount' => $item_amout_fix,
         'payment_method'=>'CARD',
         'description' => $description,
         'charge_items'=> [
             'itemId'=> $item_number,
             'description'=> $description,
             'price'=> $item_amout_fix,
             'quantity'=> '1',
         ],
         'card_number' => $request->card,
         'expiry_year' => $request->year,
         'expiry_month' => $request->month,
         'cvv' => $request->cvv,
         'save_card'     => '0',
     ])
 ]

这篇关于Api JSON解码回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆