0

You know how in PHP there's a method called file_get_content that gets the content of the page for the provided url? Is there an opposite method for it? Like, for example, file_post_content, where you can post data to external websites? Just asking for educational purposes.

jessica
  • 1,593
  • 1
  • 11
  • 29
  • 1
    PHP's file_get_contents function read data from a local/remote file. There is also a function called file_put_contents (http://php.net/file_put_contents) to write files locally. Writing files remotely is a different matter. – David Vartanian Oct 05 '15 at 21:06
  • 2
    see this Q&A's about posting data using `curl` http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code – Alex Andrei Oct 05 '15 at 21:10
  • I would echo cURL as well. You could write a function that does this too. – Twisty Oct 05 '15 at 21:27

2 Answers2

1

You can use without cURL but file_get_contents PHP this example:

$url = 'URL';
$data = array('field1' => 'value', 'field2' => 'value');
$options = array(
        'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

See the PHP website: http://php.net/manual/en/function.file-get-contents.php#102575

atiruz
  • 2,461
  • 22
  • 34
0

Could write one:

<?php
function file_post_content($url, $data = array()){
    // Collect URL. Optional Array of DATA ['name' => 'value']
    // Return response from server or FALSE
    if(empty($url)){
        return false;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 1);
    if(count($data)){
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    }
    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $svr_out = curl_exec ($ch);
    curl_close ($ch);
    return $svr_out;
}
?>
Twisty
  • 23,484
  • 1
  • 22
  • 40
  • What exactly does this do? – jessica Oct 05 '15 at 22:03
  • Using cURL, this will post to the URL provided. If no data is is included, it just does a POST and returns the results. If you pass an array to it, those would be posted to the URL. Again the result would be returned. For example, if you wanted to post a TimeZone to an API and get back the time, you could use the function like so: `$result = file_post_content("http://api.time.org/", array('q' => 'UTC'));` – Twisty Oct 05 '15 at 22:28
  • What if they have more than 1 field? – jessica Oct 05 '15 at 23:19
  • Since it's an array, you can allow it to have many fields. This function will handle it. Example: `$result = file_post_content("http://api.time.org/", array('q' => 'UTC', 'format' => 'H:i:s'));` – Twisty Oct 05 '15 at 23:56
  • I think we're both assuming that the other person knows what we're talking about. I actually means that I need to make it so that I can post to OTHER people's website, and not my own. So, for example. If I made an account on a forum, and want to sign in and get my content, I would be able to do that remotely, by using PHP as opposing to typing in my username and password to see the website. – jessica Oct 06 '15 at 00:02
  • This function could do that yet that also brings in a whole host of other various issues. The function works similar to `file_get_content()`, in that it will call a website (e.g. http://www.forum.com/) and post a username and password to the page. `$forum = file_post_content("http://www.forum.com/login.php", array('user' => 'jsmith', 'password' => 'Password2'));` This would get the response of the login.php page after the data was posted. – Twisty Oct 06 '15 at 02:34
  • Now what do you do with all that? What if the response is a 301 redirect to another page. What if the forum wants to save a cookie to your "browser" so it can remember that you're authenticated. Your question did not make it clear what you were trying to accomplish. – Twisty Oct 06 '15 at 02:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91435/discussion-between-twisty-and-jessica). – Twisty Oct 06 '15 at 02:37