如何在laravel 5.2中执行curl命令?

时间:2016-05-05 04:18:43

标签: php laravel curl laravel-5.2 guzzle

我有一个卷曲命令

curl -i -k https://anAddress.com/web/publication/add -d '{"title”:"uniqueIdxxxxxxx"}' -uexpress:Test0099887766 -H"Content-Type: application/json" -X POST

我需要在laravel中运行此CURL命令。我试过用guzzle:

$client = new Client();
        $res = $client->request('POST', 'https://annAddress.com/web/publication/add', [
            'form_params' => [
                'title' => 'xc123',
            ]
        ]);

        $result= $res->getBody();
        dd($result);

但我刚收到错误:

TooManyRedirectsException in RedirectMiddleware.php line 141:
Will not follow more than 5 redirects

1 个答案:

答案 0 :(得分:1)

  $url = 'Your API URL';

  $ch = curl_init($url);

  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec ($ch);
  $info = curl_getinfo($ch);
  $http_result = $info ['http_code'];
  curl_close ($ch);

试试这样。祝你好运