Posting parameters to a url using the POST method without using a form

前端 未结 6 1797
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 14:24

I want to post parameters to a URL using the POST method but I cannot use a form. Even if I do use a form, it would have to be auto-posted with out user interaction. Is this

相关标签:
6条回答
  • 2020-12-01 14:58

    You could use JavaScript and XMLHTTPRequest (AJAX) to perform a POST without using a form. Check this link out. Keep in mind that you will need JavaScript enabled in your browser though.

    0 讨论(0)
  • 2020-12-01 15:01

    cURL is an option, using Ajax as well eventhough solving back-end problems with the front-end isn't so neat.

    A very useful post about doing it without cURL is this one: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl

    The code to do this (untested, unimproved, from the blog post):

    function do_post_request($url, $data, $optional_headers = null)
    {
       $params = array('http' => array(
                    'method' => 'POST',
                    'content' => $data
                 ));
       if ($optional_headers !== null) {
          $params['http']['header'] = $optional_headers;
       }
       $ctx = stream_context_create($params);
       $fp = @fopen($url, 'rb', false, $ctx);
       if (!$fp) {
          throw new Exception("Problem with $url, $php_errormsg");
       }
       $response = @stream_get_contents($fp);
       if ($response === false) {
          throw new Exception("Problem reading data from $url, $php_errormsg");
       }
       return $response;
    }
    
    0 讨论(0)
  • 2020-12-01 15:02

    If you're trying to link to something, rather than do it from code you can redirect your request through: http://getaspost.appspot.com/

    0 讨论(0)
  • 2020-12-01 15:03

    How to do it without using cURL with straight-up PHP: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl

    0 讨论(0)
  • 2020-12-01 15:07

    Using jQuery.post

    $.post(
      "http://theurl.com",
      { key1: "value1", key2: "value2" },
      function(data) {
        alert("Response: " + data);
      }
    );
    
    0 讨论(0)
  • 2020-12-01 15:13

    it can be done with CURL or AJAX. The response is equally cryptic as the answer.

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