在 Laravel 中使用 Guzzle 而不是 cURL


Using Guzzle Instead Of cURL in Laravel

我在自己的课程中使用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 是处理此过程的最佳选择。

我怎样才能在古兹尔准确地做到这一点?

更新:

这是我的数据

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 可以按如下方式使用:

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

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

有关详细信息,请参阅文档。

如果您仍想使用 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/