如何使用php post请求登录Web服务

时间:2015-08-03 21:06:37

标签: php json web-services rest post

所以我需要访问包含一些json的Web服务,但为此我被告知要使用PHP POST方法首先登录Web服务。我给出了一个有3种类型/值的数组。

{
  "Username":"user",
  "password":"1234",
  "LoginClient":"user"
}

我一整天都在寻找解决方案,但是很短暂:(。

任何建议或推进正确的方向都将非常感激。 希望我已经清楚地解释了这一点。

1 个答案:

答案 0 :(得分:1)

你可以这样做:

$url = 'http://yourDomain.net/api/auth/';
$data = array('Username' => 'user', 'password' => '1234', 'LoginClient' => 'user');
$opts = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    )
);
$context  = stream_context_create($opts); //Creates and returns a stream context with any options supplied in options preset.
$response = file_get_contents($url, false, $context);

var_dump($response);

或者您可以阅读有关CURL作为发出POST请求的另一个选项。

相关问题