PHP cURL库函数

1、建立接口(apipost.php)

$user=$_POST["user"];
        $result = "";
        $password = "";
        $response = "";
        if($user == 10086){
            $result = "请求成功";
            $password = "123456";
            $response = "success";
        }else{
            $result = "请求失败";
            $password = "";
            $response = "failed";
        }
        
        $output = array(
                        "result"=>$result,
                        "password"=>$password,
                        "response"=>$response
                        );
        echo json_encode($output);

2、curl-post用法

$url="http://localhost/apipost.php";
    $data=array("user"=>"10086");
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    $output = curl_exec($ch);
    curl_close($ch);
    
    echo $output.'<hr>';

3、curl抓取网页

function request($url){
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($ch, CURLOPT_HEADER, 0);
        
        $output = curl_exec($ch);
        
        curl_close($ch);
        
        return $output;
    }
    
    echo request("http://www.baidu.com");

猜你喜欢

转载自blog.csdn.net/qq_40693416/article/details/81096497