- 使用原生的PHP函数发送GET请求:
$data = https://www.yisu.com/ask/array('param1' => 'value1', 'param2' => 'value2' ); $url = 'http://example.com/api?' . http_build_query($data); $response = file_get_contents($url);
- 使用原生的PHP函数发送POST请求:
$data = https://www.yisu.com/ask/array('param1' => 'value1', 'param2' => 'value2' ); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query($data) ) ); $context = stream_context_create($options); $response = file_get_contents('http://example.com/api', false, $context);
- 使用cURL库发送GET请求:
$ch = curl_init(); $data = https://www.yisu.com/ask/array('param1' => 'value1', 'param2' => 'value2' ); $url = 'http://example.com/api?' . http_build_query($data); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch);
- 使用cURL库发送POST请求:
$ch = curl_init(); $data = https://www.yisu.com/ask/array('param1' => 'value1', 'param2' => 'value2' ); curl_setopt($ch, CURLOPT_URL, 'http://example.com/api'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch);
- 使用Guzzle库发送GET请求:
$client = new GuzzleHttp\Client(); $data = https://www.yisu.com/ask/array('param1' => 'value1', 'param2' => 'value2' ); $response = $client->get('http://example.com/api', [ 'query' => $data ]); $body = $response->getBody();
- 使用Guzzle库发送POST请求:
$client = new GuzzleHttp\Client(); $data = https://www.yisu.com/ask/array('param1' => 'value1', 'param2' => 'value2' ); $response = $client->post('http://example.com/api', [ 'form_params' => $data ]); $body = $response->getBody();