GET/POST http://api.veapi.cn/jd/couponinfo?vekey=xxx&couponUrls=https%3A%2F%2Fcoupon.jd.com%2Filink%2FcouponActiveFront%2FlinkKey%2Ffront_index.action%3FlinkKey%3DAAROH_xIpeffAs_-naABEFoeweLETYj0ULXYaHD7zO6YtAqrF5a1zuYPQTRw3tIFNRDJiVT-__de6NqUPDnu4EyUb5C5nQ%26to%3Dwww.jd.com
使用curl函数,curl不是php原生库,需要安装才能使用
$api="http://api.veapi.cn/jd/couponinfo?vekey=xxx&couponUrls=https%3A%2F%2Fcoupon.jd.com%2Filink%2FcouponActiveFront%2FlinkKey%2Ffront_index.action%3FlinkKey%3DAAROH_xIpeffAs_-naABEFoeweLETYj0ULXYaHD7zO6YtAqrF5a1zuYPQTRw3tIFNRDJiVT-__de6NqUPDnu4EyUb5C5nQ%26to%3Dwww.jd.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api);
//curl_setopt($ch, CURLOPT_POST, true); //POST方式时启用
//curl_setopt($ch, CURLOPT_POSTFIELDS, $postData ); //POST方式时传参
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //如果使用https请启用
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //如果使用https请启用
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); //返回数据流,不直接输出
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //使用gzip压缩传输让访问更快
curl_setopt($ch, CURLOPT_TIMEOUT, 6); //允许执行的最长秒数。这里设定6S
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $result; //返回值
使用okhttp3/httpclient/jsoup/hutool,以hutool为例:
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpRequest;
public class testGetParam {
public static void main(String[] args) {
// API网址
String url = "http://api.veapi.cn/jd/couponinfo?vekey=xxx&couponUrls=https%3A%2F%2Fcoupon.jd.com%2Filink%2FcouponActiveFront%2FlinkKey%2Ffront_index.action%3FlinkKey%3DAAROH_xIpeffAs_-naABEFoeweLETYj0ULXYaHD7zO6YtAqrF5a1zuYPQTRw3tIFNRDJiVT-__de6NqUPDnu4EyUb5C5nQ%26to%3Dwww.jd.com";
// JDK 8u111版本后,若目标页面为HTTPS协议,请启用proxy用户密码鉴权
//System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
// 发送请求
String result = HttpRequest.get(url)
.timeout(10000)//设置超时,毫秒
.execute().body();
System.out.println(result);
}
}
推荐使用 requests,支持访问http,https网页:
import requests
# 要访问的API网页
target_url = "http://api.veapi.cn/jd/couponinfo?vekey=xxx&couponUrls=https%3A%2F%2Fcoupon.jd.com%2Filink%2FcouponActiveFront%2FlinkKey%2Ffront_index.action%3FlinkKey%3DAAROH_xIpeffAs_-naABEFoeweLETYj0ULXYaHD7zO6YtAqrF5a1zuYPQTRw3tIFNRDJiVT-__de6NqUPDnu4EyUb5C5nQ%26to%3Dwww.jd.com"
# 发送请求
response = requests.get(target_url)
# 获取页面内容
if response.status_code == 200:
print response.text