0

Issue I'm having is it appears that GET is being sent with file_get_contents in my execute function below. For the life of me I cannot figure out why. I have tested the api server with Postman using POST and it works correctly.

The first function is used to pass in my requests to get sent to the server. The second function is in my form_handler.php file where it builds the data to send via execute() to the server.

api_handler.php

<?php
function execute($command, $context = NULL)
{
    $url_scheme = "http://";
    if (isset($_SERVER['HTTPS']))
    {
        $url_scheme = "https://";
    }
    $url_host = $_SERVER['HTTP_HOST'];
    $url_api = "/SkedApi/";
    $url = $url_scheme.$url_host.$url_api.$command;

echo $url . "<br/>";
var_dump($context);
var_dump(headers_list());
    $response = file_get_contents($url, false, $context);
var_dump(headers_list());
echo $response;
    if ($response)
    {
        $output = json_decode($response, true);
    }
    else
    {
        $output = "error";
    }
    return $output;
}

?>

users_handler.php

$test = array ('foo' => 'bar', 'bar' => 'baz'); 
$postdata = http_build_query($test);

echo $postdata;

$opts = array (
    'http' => array (
        'method' => 'POST',
        'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
            . "Content-Length: " . strlen($postdata) . "\r\n",
        'content' => $postdata
        ));

var_dump($opts);

$context = stream_context_create($opts);

print_r($context);
$result = execute("Users", $context);

if ($result)
{
    echo "YES</br>";
}

From my apache access files:

::1 - - [04/Jul/2017:16:02:00 -0400] "POST /SkedApi/Users HTTP/1.0" 301 239
::1 - - [04/Jul/2017:16:02:00 -0400] "GET /SkedApi/Users/ HTTP/1.0" 200 196
::1 - - [04/Jul/2017:16:02:00 -0400] "POST /SkedAvailability/users_handler.php HTTP/1.1" 200 2688

Disclaimer: sorry about all the var_dumps;

keelerjr12
  • 1,693
  • 2
  • 19
  • 35
  • Possible duplicate of [Apache 301 Redirect and preserving post data](https://stackoverflow.com/questions/13628831/apache-301-redirect-and-preserving-post-data) – vhu Jul 04 '17 at 20:29

1 Answers1

0

Well your log is showing a 301 response to a POST request followed by a 200 response to the same URL with a trailing slash appended.

What happens if you change this line of your users_handler.php

$result = execute("Users", $context);

To this?

$result = execute("Users/", $context);
miknik
  • 5,748
  • 1
  • 10
  • 26
  • It worked... So now I need to figure out the best way to do redirects and pass the POST data – keelerjr12 Jul 04 '17 at 20:45
  • You could add a rewrite rule with response code 307 for all POST requests. Easy option of course is just to modify your execute function so it appends a slash to the end of the $url variable – miknik Jul 04 '17 at 21:15