file_get_contents() with POST method

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Wed Oct 07, 2009 3:31 am

Hey mlotfi63,

You can use PHP cURL functions for that.
Example:

Code: Select all

<?php

$url = "http://somehost.ext/datafile.php";

// v1, v2 - whatever the values come from ...
$post = "param1=" . $v1 . "&param2=" . $v2;

$session = curl_init($url);
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HEADER, 0);
curl_setopt($session, CURLOPT_POST, 1);
curl_setopt($session, CURLOPT_POSTFIELDS, $post);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($session);
curl_close($session);
return $data;

?>
Hope it helps ;)
Post Reply