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'        => '[email protected]',
);

$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.

5
  • Hope this could help you,stackoverflow.com/questions/611906/… Commented Jul 24, 2013 at 6: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 Commented Jul 24, 2013 at 6:45
  • Now I saw you don't want to use cURL. The other option is to use fsocketopen, something like this stackoverflow.com/questions/2367458/… Commented Jul 24, 2013 at 6:47
  • @machineaddict, I know, I need to request a POST with http_build_query not cURL. Commented Jul 24, 2013 at 6:48
  • 2
    http_build_query does only generate a string, it has nothing to do with sending requests. Commented Jul 24, 2013 at 6:49

1 Answer 1

5
<?php

$data = array(
    'name' => 'Jack',
    'surname' => 'Jackson',
    'username' => 'jackson12',
    'email' => '[email protected]',
);

$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");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your response, but where should I enter the uri (samplesite.com) I want it to be redirected to that address
May you provide additional info? Do you want send request from other domain [ for example from (example.com/yourscript.php) to (samplesite.com) ] or do you want send request to script itself?
I have updated my answer. I hope that I understand you correctly.
look at this (header("Location: samplesite.com");) I want the (samplesite.com) address with generated post request like (samplesite.com?1239owlkjsdlasjdsa)
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.