2

I need to do a POST request to an outside source with data from a file, so I tried to merge these 2 solutions I saw, but I can't seem to make them work, can you help me please?

// Submit those variables to the server
$post_data = array(
    'test' => 'foobar',
    'okay' => 'yes',
    'number' => 2
);

// Send a request to example.com 
$result = post_request('http://www.example.com/', $post_data);

if ($result['status'] == 'ok'){

    // Print headers 
    echo $result['header']; 

    echo '<hr />';

    // print the result of the whole request:
    echo $result['content'];

}
else {
    echo 'A error occured: ' . $result['error']; 
}

And in array() I'm putting

$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

So in the end I get something like this

<?php
    $post_data = array($trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);


    $result = post_request('http://www.example.com/', $post_data);

    if ($result['status'] == 'ok'){


        echo $result['header']; 

        echo '<hr />';


        echo $result['content'];

    }
    else {
        echo 'A error occured: ' . $result['error']; 
    }
?>

EDIT: I'm sorry if I wasn't clear on what I need. Actually by chance I found someone with the same need as I here stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get although I wanted to do it with php.

Basically I was asked to make a quick form inside a website in which the user writes some credentials that are saved in a .txt and that data is used to log in via POST to an affiliate external website. This gives the user the confort of being able to login to the affiliate website from the first without having to click new links.

1 Answers1

0

There's an problem with the first line of your code. Also the data attributes from the first snippet are missing. You can add them as well.

$post_data = array($trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

replace it with

$post_data = array(
    'trimmed' => file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
);

See the official documentation for declaring arrays in PHP

If you need the data from the file, you'll probably need to parse it to an array and then post it to the url. Can you post the format of the txt file, if this is the case?

Vestimir Markov
  • 330
  • 1
  • 8
  • Thank you for all your help, although I failed to achieve the goal via post, it turns out that the affiliate website form accepts logins via GET, so I achieved my goal using a simple GET redirect. I appreciate your help. Cheers – user2156764 Mar 11 '13 at 17:57