zoukankan      html  css  js  c++  java
  • 使用PHP的curl扩展实现跨域post请求,以及file_get_contents()百度短网址例子

    <?php
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php");
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $data=array('url'=>'http://www.cnblogs.com/zuoxiaobing/');
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $strRes=curl_exec($ch);
    curl_close($ch);
    $arrResponse=json_decode($strRes,true);
    if($arrResponse['status']==0)
    {
    /**错误处理*/
    echo iconv('UTF-8','GBK',$arrResponse['err_msg'])."
    ";
    }
    /** tinyurl */
    echo$arrResponse['tinyurl']."
    ";
    ?>

     2.使用file_get_contents();

    <?php
    $data=array(
        'url'=>'http://www.cnblogs.com/zuoxiaobing/'
    );
    $data = http_build_query($data);
    $opts = array('http' =>
            array(
                    'method'  => 'POST',
                    'header'  => "Content-type: application/x-www-form-urlencoded
    "
                    ."Content-Length: " . strlen($data) . "
    ", 
                        //    "Authorization: Basic ".base64_encode("$https_user:$https_password")."
    ",
                    'content' => $data,
                    'timeout' => 60
            )
    );
     
    $context  = stream_context_create($opts);
    $url = 'http://dwz.cn/create.php';
    $result = file_get_contents($url, false, $context);
    echo $result;
  • 相关阅读:
    同余 扩展欧几里得
    185. [USACO Oct08] 挖水井
    Dijkstra算法
    Floyed算法
    codves——1079 回家
    codves——1021 玛丽卡
    codves——5960 信使
    计算几何基础
    【正睿oi省选十连测】第一场
    [APIO2012]守卫
  • 原文地址:https://www.cnblogs.com/zuoxiaobing/p/3666650.html
Copyright © 2011-2022 走看看