PHP: Simulate XHR using cURL

前端 未结 2 1064
太阳男子
太阳男子 2020-12-30 13:58

Today I\'m trying to make a cron job for some forum login to check for online stats. The login.php script accepts an ajax request with the form submitted values: user, passw

相关标签:
2条回答
  • 2020-12-30 14:30

    In the PHP api to curl you can use:

    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        "Host" => "someloginserver.com",
        "User-Agent" => "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
        "Accept" => "application/json, text/javascript, */*; q=0.01",
        "Accept-Language" => "en-us,en;q=0.5",
        "Accept-Encoding" => "gzip, deflate",
        "Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
        "Keep-Alive" => "115",
        "Connection" => "keep-alive",
        "X-Requested-With" => "XMLHttpRequest",
        "Referer" => "http://someloginserver.com/sendlogin.php"
    ));
    

    But your actual problem might be the Cookie:, which I've excluded above. Setup your cURL request with a COOKIEJAR. Make one faux request to get a current session value, and only afterwards send your actual XHR request.

    0 讨论(0)
  • 2020-12-30 14:33

    That array format doesn't work. Curl does not accept associative arrays. Each element must be a string in the following format:

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Host: www.somehost.com",
            "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
            "Accept: application/json, text/javascript, */*; q=0.01",
            "Accept-Language: en-us,en;q=0.5",
            "Accept-Encoding: gzip, deflate",
            "Connection: keep-alive",
            "X-Requested-With: XMLHttpRequest",
            "Referer: http://www.somehost.com/"
        ));
    
    0 讨论(0)
提交回复
热议问题