简体   繁体   中英

http post in php without curl using username, password, param1, param2(string array)?

I have a form with a host link and the url that i can post to. The method works fine online, i want to use php to enter data automatically, how do i do so without curl

it would be passed like this (Username, Password, Example, Example2)

Example 2 is a string array of data with required parameter names

if there is nothing passed at all the error will state "Login Error"

if Example 2 is empty it will display "Root element is missing."

See also: How to post data in PHP using file_get_contents?

Basically you can use file_get_contents() and stream_context_create() for issuing a POST request. In your case:

$post = http_build_query(array(
    "username" => "user",
    "password" => "pw",
    "example" => "...",
));

$context = stream_context_create(array("http"=>array(
     "method" => "POST",
     "header" => "Content-Type: application/x-www-form-urlencoded\r\n" .
                 "Content-Length: ". strlen($post) . "\r\n",  
     "content" => $post,
))); 

$page = file_get_contents("http://example.com/login", false, $context);

Just open up a socket connection to port 80, send the headers (you can use livehttp headers if you want to cheat on creating the headers, just watch while posting from the browser and copy it). Then send the data after the headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM