PHP API代理示例
流程:请求 API → 解析 JSON → 处理错误 → 使用返回代理访问目标站。
file_get_contents + stream 上游代理
1<?php2$apiUrl = 'http://ip.16yun.cn:817/myip/pl/<ORDER_ID>/?s=<ORDER_SIGN>&u=<USER>&format=json';34$json = file_get_contents($apiUrl);5if ($json === false) {6die('API 请求失败');7}8$arr = json_decode($json, true);9if (!is_array($arr) || count($arr) === 0) {10die('API 返回为空');11}12$ip = $arr[0]['ip'];13$port = $arr[0]['port'];1415$proxy = "tcp://{$ip}:{$port}";16$opts = [17'http' => [18'proxy' => $proxy,19'method' => 'GET',20'request_fulluri' => true,21'timeout' => 10,22],23];24$ctx = stream_context_create($opts);25$result = file_get_contents('https://httpbin.org/ip', false, $ctx);26var_dump($result);27?>
curl
1<?php2$apiUrl = 'http://ip.16yun.cn:817/myip/pl/<ORDER_ID>/?s=<ORDER_SIGN>&u=<USER>&format=json';3$json = file_get_contents($apiUrl);4$arr = json_decode($json, true);5$ip = $arr[0]['ip'];6$port = $arr[0]['port'];78$ch = curl_init();9curl_setopt($ch, CURLOPT_URL, 'https://httpbin.org/ip');10curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);11curl_setopt($ch, CURLOPT_PROXY, $ip);12curl_setopt($ch, CURLOPT_PROXYPORT, $port);13curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);14curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);15curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);16$data = curl_exec($ch);17curl_close($ch);18echo $data;19?>