<?php namespace app\controller; use app\BaseController; use GatewayClient\Gateway; use GuzzleHttp\Client; use GuzzleHttp\Pool; use think\facade\Log; use GuzzleHttp\Psr7\Request; class Index extends BaseController { public function index() { //echo $this->request->post(); Gateway::$registerAddress = '127.0.0.1:1238'; $res = Gateway::sendToAll(json_encode(['name' => 'chenlong', 'age' => 100])); //print_r($res); $data = file_get_contents("php://input"); Log::write(input('param.'), 'notice1'); Log::write(input('get.'), 'notice2'); Log::write(input('post.'), 'notice3'); Log::write($_POST, 'notice4'); Log::write($_REQUEST, 'notice5'); Log::write($data, 'notice6'); return json($data); //echo 3333; } public function curl() { $params = [ 'key' => '*****', 'userid' => 'yemeishu' ]; $client = new Client(['timeout' => 1.0]); $options = json_encode($params, JSON_UNESCAPED_UNICODE); $data = [ 'body' => $options, //'headers' => ['content-type' => 'application/json'] //'headers' => ['content-type' => 'text/html'] ]; // 发送 post 请求 //$url= 'https://baidu.com'; $url = 'http://127.0.0.1/index.php'; //$url= 'http://share.topdinghui.com/index.php'; //$response = $client->post('http://47.112.137.157:8090/api.php/platform/list', $data); $response = $client->post($url, $data); //print_r($response); $callback = $response->getBody()->getContents(); print_r($callback); } public function hello($name = 'ThinkPHP6') { return 'hello,' . $name; } public function test() { $urls=[ 'http://127.0.0.1/index.php', 'http://127.0.0.1/index1.php', 'http://share.topdinghui.com/index.php', 'http://47.112.137.157:8090/api.php/platform/list', ]; $client = new Client(); $requests = function ($urls) { $uri = 'http://127.0.0.1/index.php'; //$uri = 'http://47.112.137.157:8090/api.php/platform/list'; foreach ($urls as $v){ yield new Request('GET', $v, [ 'headers' => ['content-type' => 'application/xhtml+xml'], 'timeout' => 5 //'decode_content' => false ]); } }; $pool = new Pool($client, $requests($urls), [ 'concurrency' => 5, 'fulfilled' => function ($response, $index) { $body = $response->getBody()->getContents(); print_r($body); echo 'successful --' . $index . PHP_EOL; echo '=========================================================='. PHP_EOL; // this is delivered each successful response }, 'rejected' => function ($reason, $index) { echo 'failed --' . $index . PHP_EOL; // this is delivered each failed request }, ]); // Initiate the transfers and create a promise $promise = $pool->promise(); // Force the pool of requests to complete. $promise->wait(); } //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies protected function curl_request($url,$post='',$cookie='', $returnCookie=0){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_AUTOREFERER, 1); curl_setopt($curl, CURLOPT_REFERER, "http://XXX"); if($post) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); } if($cookie) { curl_setopt($curl, CURLOPT_COOKIE, $cookie); } curl_setopt($curl, CURLOPT_HEADER, $returnCookie); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); if (curl_errno($curl)) { return curl_error($curl); } curl_close($curl); if($returnCookie){ list($header, $body) = explode("\r\n\r\n", $data, 2); preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches); $info['cookie'] = substr($matches[1][0], 1); $info['content'] = $body; return $info; }else{ return $data; } } public function testCurl(){ $uri = 'http://127.0.0.1/index.php'; echo $this->curl_request($uri); } }
php 的 curl请求
最新推荐文章于 2024-03-20 14:06:10 发布