http post在php没有curl使用用户名,密码,param1,param2(字符串数组)? [英] http post in php without curl using username, password, param1, param2(string array)?

查看:298
本文介绍了http post在php没有curl使用用户名,密码,param1,param2(字符串数组)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含主机链接和可以发布到的网址的表单。该方法工作正常在线,我想使用PHP自动输入数据,我怎么这样,没有curl



它会像这样传递(用户名,密码,示例,Example2)



示例2是一个包含必需参数名称的数据的字符串数组



如果示例2为空,它将显示根元素缺失。


$ b < class =h2_lin>解决方案

另请参阅:如何使用file_get_contents在PHP中发布数据?



基本上你可以使用 file_get_contents() stream_context_create()发出POST请求。在您的情况下:

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

$ context = stream_context_create(array(http=> array(
method=>POST,
header=&类型:application / x-www-form-urlencoded\r\\\

Content-Length:。strlen($ post)。\r\\\

content=> $ post,
)));

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


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);

这篇关于http post在php没有curl使用用户名,密码,param1,param2(字符串数组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆