3

I need to create a POST request with http_build_query. Following are my codes:

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

At the moment it generates a request like GET, but I need POST. Consider that I do not want to use curl, ... only http_build_query.

  • Hope this could help you,http://stackoverflow.com/questions/611906/http-post-with-url-query-parameters-good-idea-or-not – dreamweiver Jul 24 '13 at 06:45
  • What you did there is not `POST`, is `GET`. To create a `POST` request use `cURL`, something like this [http://www.php.net/manual/en/function.curl-exec.php#98628](http://www.php.net/manual/en/function.curl-exec.php#98628) – machineaddict Jul 24 '13 at 06:45
  • Now I saw you don't want to use cURL. The other option is to use [fsocketopen](http://php.net/manual/en/function.fsockopen.php), something like this http://stackoverflow.com/questions/2367458/php-post-data-with-fsockopen – machineaddict Jul 24 '13 at 06:47
  • @machineaddict, I know, I need to request a POST with http_build_query not cURL. –  Jul 24 '13 at 06:48
  • 2
    http_build_query does only generate a string, it has nothing to do with sending requests. – Thomas Lauria Jul 24 '13 at 06:49

1 Answers1

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");
VenZell
  • 105
  • 1
  • 12
  • Thanks for your response, but where should I enter the uri (http://samplesite.com) I want it to be redirected to that address –  Jul 24 '13 at 07:33
  • May you provide additional info? Do you want send request from other domain [ for example from (http://example.com/yourscript.php) to (http://samplesite.com) ] or do you want send request to script itself? – VenZell Jul 24 '13 at 07:43
  • I have updated my answer. I hope that I understand you correctly. – VenZell Jul 24 '13 at 08:07
  • look at this (header("Location: http://samplesite.com");) I want the (http://samplesite.com) address with generated post request like (http://samplesite.com?1239owlkjsdlasjdsa) –  Jul 24 '13 at 08:18
  • 1
    As far as I know, in this situation, there's only one solution: you have to create a form as HTML - then use javascript to submit it. And you can't generate such string for a POST request... – VenZell Jul 24 '13 at 10:06