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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37042271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:43:44  来源:igfitidea点击:

How to execute curl commands in laravel 5.2?

phplaravelcurllaravel-5.2guzzle

提问by kevinabraham

I have a curl command

我有一个 curl 命令

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

I need to run this CURL command in laravel. I tried using guzzle:

我需要在 Laravel 中运行这个 CURL 命令。我尝试使用狂饮:

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

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

but I just get an error:

但我只是收到一个错误:

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

回答by Sunil

  $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);

Try like this. Good luck

像这样尝试。祝你好运