在Laravel中使用Guzzle而不是cURL

时间:2015-11-12 14:40:13

标签: php laravel curl guzzle

我在自己的班级上使用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"

当我像下面的代码一样张贴时,我得到一个空白页面。

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

2 个答案:

答案 0 :(得分:0)

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

$client = new GuzzleHttp\Client();

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

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

可从documentation获取更多信息。

答案 1 :(得分:-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/