2

I have 2 pages say abc.php and def.php. When abc.php sends 2 values [id and name] to def.php, it shows a message "Value received". Now how can I send those 2 values to def.php without using form in abc.php and get the "Value received" message from def.php? I can't use form because when user frequently visits the abc.php file, the script should automatically work and get the message "Value received" from def.php. Please see my example code:

abc.php:

 <?php 
  $id="123";
  $name="blahblah";
   //need to send the value to def.php & get value from that page
  // echo $value=Print the "Value received" msg from def.php;     
 ?>

def.php:

 <?php
  $id=$_GET['id'];
  $name=$_GET['name'];
  if(!is_null($id)&&!is_null($name))
  {  echo "Value received";}
  else{echo "Not ok";}
 ?>

Is there any kind heart who can help me solve the issue?

1

5 Answers 5

6

First make up your mind : do you want GET or POST parameters.

Your script currently expects them to be GET parameters, so you can simply call it (provided that URL wrappers are enabled anyway) using :

$f = file_get_contents('http://your.domain/def.php?id=123&name=blahblah');

To use the curl examples posted here in other answers you'll have to alter your script to use $_POST instead of $_GET.

Sign up to request clarification or add additional context in comments.

1 Comment

WOW...just a single line code and solve all of my problems..many thanks wimvds.Also many thanks all the people who guide me to solve..thx bro
3

You can try without cURL (I havent tried though):

Copy pasted from : POSTing data without cURL extension

// Your POST data
$data = http_build_query(array(
    'param1' => 'data1',
    'param2' => 'data2'
));

// Create HTTP stream context
$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $data
    )
));

// Make POST request
$response = file_get_contents('http://example.com', false, $context);

3 Comments

thanks trix, but when i run the script then it's showing "Not Ok" that mean code does not working..could you pls help...
Dear trix,pls give some time for me..i think we are near to solve the problem.But the response is showing "Not ok".Please see my code and suggest me how i can get the value "Value received";
replace $_GET with $_POST in def.php, also check wimvds answer
3

Taken from the examples page of php.net:

// create curl resource
$ch = curl_init();

// set url
curl_setopt( $ch, CURLOPT_URL, "example.com/abc.php");

//return the transfer as a string
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec( $ch );

// close curl resource to free up system resources
curl_close( $ch );  

Edit: To send parameters

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, array('var1=foo', 'var2=bar'));

2 Comments

How i send the parameters to def.php?could you pls help??
thx for effort..but value not received on def.php and don't show the value..pls help
1

use CURL or Zend_Http_Client.

3 Comments

could u pls give and example to do it?
Yeroon copy pasted an example.
thx for helping me...but how i send value to def.php and get the output from def.php?
0
<?php
$method = 'GET'; //change to 'POST' for post method
$url = 'http://localhost/browse/';
$data = array(
    'manufacturer' => 'kraft',
    'packaging_type' => 'bag'
    );

if ($method == 'POST'){
//Make POST request
    $data = http_build_query($data);
    $context = stream_context_create(array(
        'http' => array(
            'method' => "$method",
            'header' => 'Content-Type: application/x-www-form-urlencoded',
            'content' => $data)
        )
    );
    $response = file_get_contents($url, false, $context);
}
else {
// Make GET request
    $data = http_build_query($data, '', '&');
    $response = file_get_contents($url."?".$data, false);
}
echo $response;
?>

get inspired by trix's answer, I decided to extend that code to cater for both GET and POST method.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.