【问题标题】:Using Guzzle Instead Of cURL in Laravel在 Laravel 中使用 Guzzle 代替 cURL
【发布时间】:2016-02-13 22:17:08
【问题描述】:

我在自己的课程中使用 Virtual Pos。但我想决定将我的项目转换为 laravel 项目。

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->_server);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vPOSData);

$ch = curl_exec($ch);
@curl_close ($ch);
echo $ch;

当我直接在 Laravel 中使用此代码时,它不起作用。当我检查互联网时,Guzzle 是处理此过程的最佳选择。

我怎样才能在 Guzzle 中做到这一点?

更新:

这是我的数据

array:20 [
"clientid" => "*********"
"amount" => "27.87"
"oid" => 14532858
"okUrl" => "http://laravel/tr/order/**1/success"
"failUrl" => "http://laravel/tr/order/**1/fail"
"islemtipi" => "Auth"
"taksit" => ""
"commission" => null
"storetype" => "3d_pay"
"cardHolder" => "cihan küsmez"
"pan" => "4531****31442283"
"Ecom_Payment_Card_ExpDate_Month" => "12"
"Ecom_Payment_Card_ExpDate_Year" => "18"
"cv2" => "001"
"rnd" => "0.88093200 1447345882"
"hash" => "1phjMQWYUkmJRXj283lonh7GAZE="
"lang" => "tr"
"currency" => 949
"customerIP" => "127.0.0.1"
"vpos_name" => "****** vPOS"

]

当我像下面的代码一样使用 guzzle 发布时,我得到一个空白页。

    $client = new Client();
    return $client->post($this->_server, $vPOSData);

【问题讨论】:

  • 你有没有尝试过,或者你只是在这里“给我密码!!”心态?
  • 首先你需要说明它为什么不工作。因为如果您打算将 Guzzle 与 cURL 一起使用,它很可能会在幕后执行您现在正在尝试的相同操作,但它可能仍然无法正常工作。
  • @JosephSilber 你可以查看我的个人资料。我不是在乞求代码。 :) 我想我无法说出我在想象什么。

标签: php laravel curl guzzle


【解决方案1】:

如果你还想用 cURL 请求

首先确保您已启用 cURL 扩展

cURL 请求函数

function httpPost($url,$params)
{
  $postData = '';
   //create name value pairs seperated by &
   foreach($params as $k => $v) 
   { 
      $postData .= $k . '='.$v.'&'; 
   }
   rtrim($postData, '&');

    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;

}

功能使用方法

$params = array(
   "name" => "Ravishanker Kusuma",
   "age" => "32",
   "location" => "India"
);

echo httpPost("http://hayageek.com/examples/php/curl-examples/post.php",$params);

参考:http://hayageek.com/php-curl-post-get/

【讨论】:

  • 亲爱的 Rameez,我已经在我的项目中使用了 cURL。我转换为 Laravel,这就是我以前的代码存在问题的原因。可能是 CSRF 保护导致了问题?
  • @CihanKüsmez 发布您遇到的错误,如果您没有提供有关问题的信息,我们无法想象出什么不起作用。
  • Laravel 是 php,转换为 laravel 后不需要更改 curl 函数,为什么 csrf 会导致问题,因为 curl 是外部请求?
【解决方案2】:

在最简单的用例中,Guzzle 可以按如下方式使用:

$client = new GuzzleHttp\Client();

$response = $client->post($uri, [
    'form_params' = > $array_of_parameters,
]);

$responseGuzzleHttp\Psr7\Response 的一个实例,是Psr\Http\Message\ResponseInterface 的一个实现。

更多信息请访问documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2017-04-04
    • 2017-06-06
    • 2016-03-06
    • 2019-12-17
    • 2018-07-02
    相关资源
    最近更新 更多