resource curl_init ([ string $url = NULL ] )
<?php // 初始化cURL会话 $ch = curl_init(); <p>// 设置请求的URL和返回结果作为字符串<br> curl_setopt($ch, CURLOPT_URL, "<a rel="noopener" target="_new" class="" href="http://api.example.com/users">http://api.example.com/users</a>");<br> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);</p> <p>// 执行cURL请求,获取响应内容<br> $response = curl_exec($ch);</p> <p>// 错误检测<br> if(curl_errno($ch)){<br> $error_message = curl_error($ch);<br> echo "cURL Error: " . $error_message;<br> }</p> <p>// 关闭cURL会话<br> curl_close($ch);</p> <p>// 处理响应数据<br> if($response){<br> $data = json_decode($response, true);<br> if($data){<br> foreach($data as $user){<br> echo "User ID: " . $user['id'] . "<br>";<br> echo "User Name: " . $user['name'] . "<br>";<br> echo "User Email: " . $user['email'] . "<br><br>";<br> }<br> } else {<br> echo "Invalid response.";<br> }<br> } else {<br> echo "No response received.";<br> }<br> ?>
相关标签:
cURL