3

我需要使用http_build_query创建一个POST请求。以下是我的代码:

$uri_args = array (
  'name'         => 'Jack',
  'surname'      => 'Jackson',
  'username'     => 'jackson12',
  'email'        => 'Jack@mail.com',
);

$uri = http_build_query($uri_args);

header("Location: http://samplesite.com?$uri");

目前它会生成一个类似 GET 的请求,但我需要 POST。考虑到我不想使用 curl,......只有http_build_query

4

1 回答 1

4
<?php

$data = array(
    'name' => 'Jack',
    'surname' => 'Jackson',
    'username' => 'jackson12',
    'email' => 'Jack@mail.com',
);

$query = http_build_query($data); 

// create context
$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL,
        'content' => $query,
    ),
));

// send request and collect data    
$response = file_get_contents(
    $target = "http://example.com/target.php",
    $use_include_path = false,
    $context);

//some actions with $response
//...

// redirect
header("Location: http://samplesite.com");
于 2013-07-24T07:27:10.757 回答