How do I send a POST request with PHP?

前端 未结 13 1548
暗喜
暗喜 2020-11-21 05:40

Actually I want to read the contents that come after the search query, when it is done. The problem is that the URL only accepts POST methods, and it does not t

13条回答
  •  我在风中等你
    2020-11-21 05:44

    I'd like to add some thoughts about the curl-based answer of Fred Tanrikut. I know most of them are already written in the answers above, but I think it is a good idea to show an answer that includes all of them together.

    Here is the class I wrote to make HTTP-GET/POST/PUT/DELETE requests based on curl, concerning just about the response body:

    class HTTPRequester {
        /**
         * @description Make HTTP-GET call
         * @param       $url
         * @param       array $params
         * @return      HTTP-Response body or an empty string if the request fails or is empty
         */
        public static function HTTPGet($url, array $params) {
            $query = http_build_query($params); 
            $ch    = curl_init($url.'?'.$query);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, false);
            $response = curl_exec($ch);
            curl_close($ch);
            return $response;
        }
        /**
         * @description Make HTTP-POST call
         * @param       $url
         * @param       array $params
         * @return      HTTP-Response body or an empty string if the request fails or is empty
         */
        public static function HTTPPost($url, array $params) {
            $query = http_build_query($params);
            $ch    = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
            $response = curl_exec($ch);
            curl_close($ch);
            return $response;
        }
        /**
         * @description Make HTTP-PUT call
         * @param       $url
         * @param       array $params
         * @return      HTTP-Response body or an empty string if the request fails or is empty
         */
        public static function HTTPPut($url, array $params) {
            $query = \http_build_query($params);
            $ch    = \curl_init();
            \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
            \curl_setopt($ch, \CURLOPT_HEADER, false);
            \curl_setopt($ch, \CURLOPT_URL, $url);
            \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
            \curl_setopt($ch, \CURLOPT_POSTFIELDS, $query);
            $response = \curl_exec($ch);
            \curl_close($ch);
            return $response;
        }
        /**
         * @category Make HTTP-DELETE call
         * @param    $url
         * @param    array $params
         * @return   HTTP-Response body or an empty string if the request fails or is empty
         */
        public static function HTTPDelete($url, array $params) {
            $query = \http_build_query($params);
            $ch    = \curl_init();
            \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
            \curl_setopt($ch, \CURLOPT_HEADER, false);
            \curl_setopt($ch, \CURLOPT_URL, $url);
            \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
            \curl_setopt($ch, \CURLOPT_POSTFIELDS, $query);
            $response = \curl_exec($ch);
            \curl_close($ch);
            return $response;
        }
    }
    

    Improvements

    • Using http_build_query to get the query-string out of an request-array.(you could also use the array itself, therefore see: http://php.net/manual/en/function.curl-setopt.php)
    • Returning the response instead of echoing it. Btw you can avoid the returning by removing the line curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);. After that the return value is a boolean(true = request was successful otherwise an error occured) and the response is echoed. See: http://php.net/en/manual/function.curl-exec.php
    • Clean session closing and deletion of the curl-handler by using curl_close. See: http://php.net/manual/en/function.curl-close.php
    • Using boolean values for the curl_setopt function instead of using any number.(I know that any number not equal zero is also considered as true, but the usage of true generates a more readable code, but that's just my opinion)
    • Ability to make HTTP-PUT/DELETE calls(useful for RESTful service testing)

    Example of usage

    GET

    $response = HTTPRequester::HTTPGet("http://localhost/service/foobar.php", array("getParam" => "foobar"));
    

    POST

    $response = HTTPRequester::HTTPPost("http://localhost/service/foobar.php", array("postParam" => "foobar"));
    

    PUT

    $response = HTTPRequester::HTTPPut("http://localhost/service/foobar.php", array("putParam" => "foobar"));
    

    DELETE

    $response = HTTPRequester::HTTPDelete("http://localhost/service/foobar.php", array("deleteParam" => "foobar"));
    

    Testing

    You can also make some cool service tests by using this simple class.

    class HTTPRequesterCase extends TestCase {
        /**
         * @description test static method HTTPGet
         */
        public function testHTTPGet() {
            $requestArr = array("getLicenses" => 1);
            $url        = "http://localhost/project/req/licenseService.php";
            $this->assertEquals(HTTPRequester::HTTPGet($url, $requestArr), '[{"error":false,"val":["NONE","AGPL","GPLv3"]}]');
        }
        /**
         * @description test static method HTTPPost
         */
        public function testHTTPPost() {
            $requestArr = array("addPerson" => array("foo", "bar"));
            $url        = "http://localhost/project/req/personService.php";
            $this->assertEquals(HTTPRequester::HTTPPost($url, $requestArr), '[{"error":false}]');
        }
        /**
         * @description test static method HTTPPut
         */
        public function testHTTPPut() {
            $requestArr = array("updatePerson" => array("foo", "bar"));
            $url        = "http://localhost/project/req/personService.php";
            $this->assertEquals(HTTPRequester::HTTPPut($url, $requestArr), '[{"error":false}]');
        }
        /**
         * @description test static method HTTPDelete
         */
        public function testHTTPDelete() {
            $requestArr = array("deletePerson" => array("foo", "bar"));
            $url        = "http://localhost/project/req/personService.php";
            $this->assertEquals(HTTPRequester::HTTPDelete($url, $requestArr), '[{"error":false}]');
        }
    }
    

提交回复
热议问题