PHP CURL

<?php
function getUrl($url){
    $header = [‘Content-type:application/json;‘,‘Accept:application/json‘];
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url); //设置抓取的URL
    //设置获取信息以文件流的形式
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header); //设置请求头
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output,true);
    return $output;
}
function postUrl($url, $data){
    $data  = json_encode($data);
    $header = ["Content-type:application/x-www-form-urlencoded","Accept:application/json"];
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output,true);
}
function postFileGetContents($url,$data){
    $stream = [
        ‘http‘=>[
            ‘method‘=>‘POST‘,
            ‘header‘=>"Content-type:application/x-www-form-urlencoded",
            ‘content‘=>http_build_query($data),
            ‘timeout‘=>20
        ]
    ];
    $content = stream_context_create($stream);
    $output = file_get_contents($url,false,$content);
    return json_decode($output,true);
}
$url = "/api/getTypeList.php";
$data  = ["classid"=>"a2"];
$data  = ["page"=>1,‘limit‘=>5];
//var_dump(getUrl($url));
//var_dump(postUrl($url,$data));
//var_dump(postFileGetContents($url,$data));

PHP CURL

上一篇:如何在Ubuntu 16.04上安装和保护MongoDB


下一篇:头条二面:详述一条 SQL 的执行流程