zoukankan      html  css  js  c++  java
  • php 模拟get和post提交方法[解决ajax跨域问题]

    get:

    $url = "http://www.111cn.net /index.php?a=b&c=d&e=f&g=" . urlencode('王璐个人博客');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $output = curl_exec($ch);
    curl_close($ch);
    var_dump($output);

    post:

    $url = "http://www.111cn.net/ index.php";
    $params = "a=b&c=d&e=f&g=" . urlencode('王璐个人博客');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
     
    curl_setopt($ch, CURLOPT_POST, 1);    // post 提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
     
    $output = curl_exec($ch);
    curl_close($ch);
    var_dump($output);

    当请求https的数据时,会要求证书,这时候,加上下面这两个参数,规避ssl的证书检查

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  • 相关阅读:
    configparser模块
    xml文件解析
    shutil模块 + shelve模块 二合一版
    hashlib模块
    subprocess模块和sys模块
    re模块
    os模块
    random模块
    time模块、datetime模块讲解
    洛谷P3414 SAC#1
  • 原文地址:https://www.cnblogs.com/hgj123/p/4485802.html
Copyright © 2011-2022 走看看