【问题标题】:POST request only with http_build_query in phpPOST 请求仅在 php 中使用 http_build_query
【发布时间】:2013-07-24 06:40:41
【问题描述】:

我需要使用 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

【问题讨论】:

标签: php post http-post


【解决方案1】:
<?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");

【讨论】:

  • 感谢您的回复,但我应该在哪里输入 uri (samplesite.com) 我希望将其重定向到该地址
  • 您可以提供更多信息吗?您想从其他域发送请求 [例如从 (example.com/yourscript.php) 到 (samplesite.com)] 还是要将请求发送到脚本本身?
  • 我已经更新了我的答案。希望我的理解正确。
  • 看看这个 (header("Location: samplesite.com");) 我想要 (samplesite.com) 地址和生成的 post 请求,例如 (samplesite.com?1239owlkjsdlasjdsa)
  • 据我所知,在这种情况下,只有一个解决方案:您必须创建一个 HTML 表单 - 然后使用 javascript 提交它。而且您不能为 POST 请求生成这样的字符串...
猜你喜欢
  • 1970-01-01
  • 2021-11-30
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
相关资源
最近更新 更多