【问题标题】:Sending variables to another application and get the response from that?将变量发送到另一个应用程序并从中获取响应?
【发布时间】:2018-06-01 12:37:16
【问题描述】:

我需要将数据发送到不同应用程序的另一个网页,它会发送一些我需要在进一步说明中使用的 json 数据。

我需要向该网页发送一些基本信息,例如cus_name, cus_email, cus_phone,这将以 json 格式发送一些数据。

我对如何捕获 json 响应有了基本的了解:就像那样,

$client = new Client();
$body = $client->get('https://securepay.google.com/gwprocess/v3/api.php')->getBody();
$data = json_decode($body);
return redirect($data->GatewayPageURL);

我如何将这些变量发送到同一控制器并捕获响应? 提前致谢。

【问题讨论】:

  • 设计一个Web服务/Rest API来实现这个东西。

标签: php laravel api curl laravel-5


【解决方案1】:

您可以通过两种方式发送查询字符串参数。

将它们包含在 URI 本身中。

$response = $client->request('GET', 'https://securepay.google.com/gwprocess/v3/api.php?cus_name=name&cus_email=email&cus_phone=phone');

或者

使用 query 请求选项作为数组指定它们

$response = $client->request('GET', 'https://securepay.google.com/gwprocess/v3/api.php', [
    'query' => [
                'cus_name' => 'name', 
                'cus_email' => 'email', 
                'cus_phone' => 'phone'
               ]
]);

或者对于发布请求:

   $response = $client->request('POST', 'https://securepay.google.com/gwprocess/v3/api.php', [
        'form_params' => [
                        'cus_name' => 'name', 
                        'cus_email' => 'email', 
                        'cus_phone' => 'phone'
        ]
    ]);

然后得到响应:

 $result = json_decode($response->getBody());

【讨论】:

  • 服务器无法接受“查询”。还有其他方法可以发送这些参数吗?
  • 解释更多关于服务器的信息。如果服务器不排除查询参数,恐怕它不是 API 提供者,您将不得不弄清楚服务器如何响应 HTTP 请求并使用 guzzle 模仿那些
  • 服务器响应如:object(stdClass)#590 (10) { ["status"]=> string(6) "FAILED" ["failedreason"]=> string(19) "Invalid Information" ["sessionkey"]=> string(0) "" ["gw"]=> object(stdClass)#588 (6) { ["visa"]=> string(0) "" ["master"]=> string(0) "" ["amex"]=> string(0) "" ["othercards"]=> string(0) "" ["internetbanking"]=> string(0) "" ["mobilebanking"]=> string(0) "" } ["redirectGatewayURL"]=> string(0) "" ["GatewayPageURL"]=> string(0) "" ["directPaymentURL"]=> string(0) "" ["storeBanner"]=> string(0) "" ["storeLogo"]=> string(0) "" ["desc"]=> array(0) { } }
  • 服务器响应无所谓,你需要先弄清楚服务器如何接受参数。如果它确实接受了所有
  • 查看当我通过表单提交上述参数时,它接受它并向我发送实际响应,但我想通过 guzzle 来完成,这就是原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 2016-02-03
  • 2012-11-13
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
相关资源
最近更新 更多