How do I send a POST request with PHP?

前端 未结 13 1554
暗喜
暗喜 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 06:03

    If you by any chance are using Wordpress to develop your app (it's actually a convenient way to get authorization, info pages etc even for very simple stuff), you can use the following snippet:

    $response = wp_remote_post( $url, array('body' => $parameters));
    
    if ( is_wp_error( $response ) ) {
        // $response->get_error_message()
    } else {
        // $response['body']
    }
    

    It uses different ways of making the actual HTTP request, depending on what is available on the web server. For more details, see the HTTP API documentation.

    If you don't want to develop a custom theme or plugin to start the Wordpress engine, you can just do the following in an isolated PHP file in the wordpress root:

    require_once( dirname(__FILE__) . '/wp-load.php' );
    
    // ... your code
    

    It won't show any theme or output any HTML, just hack away with the Wordpress APIs!

提交回复
热议问题