php curl: I need a simple post request and retrival of page example

前端 未结 8 1037
走了就别回头了
走了就别回头了 2020-11-27 17:18

I would like to know how to send a post request in curl and get the response page.

相关标签:
8条回答
  • 2020-11-27 18:14

    Check out my code in this post

    https://stackoverflow.com/a/56027033/6733212

    <?php
    if (!function_exists('curl_version')) {
        exit("Enable cURL in PHP");
    }
    
    $url = "https://www.google.com/";
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_POSTFIELDS => "",
        CURLOPT_HTTPHEADER => array(
            "Accept: */*",
            "Cache-Control: no-cache",
            "Connection: keep-alive",
            "Host: " . url($url),
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
            "accept-encoding: gzip, deflate",
            "cache-control: no-cache",
        ),
    ));
    
    function url($url)
    {
        $result = parse_url($url);
        return $result['host'];
    }
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo "<textarea>" . $response . "</textarea>";
    
    }
    

    http://codepad.org/YE6fyzCA

    0 讨论(0)
  • 2020-11-27 18:19

    try the one in the comments: http://php.net/manual/en/curl.examples-basic.php

    (but add curl_setopt($ch, CURLOPT_POST, 1) to make it a post instead of get)

    or this example: http://php.dzone.com/news/execute-http-post-using-php-cu

    0 讨论(0)
提交回复
热议问题