在PHP中,可以使用cURL库或者file_get_contents()函数来发起GET请求。下面是两种方法的示例:
使用cURL库发起GET请求:<?phpfunction httpGet($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $result = curl_exec($ch); curl_close($ch); return $result;}$url = "https://api.example.com/data";$response = httpGet($url);echo $response;?>使用file_get_contents()发起GET请求:<?phpfunction httpGet($url) { $context = stream_context_create(['http'=>['method'=>'GET']]); $response = file_get_contents($url, false, $context); return $response;}$url = "https://api.example.com/data";$response = httpGet($url);echo $response;?>这两个示例都会向指定的URL发起GET请求,并将响应内容输出。你可以根据自己的需求选择合适的方法。


