Page 1 of 1

Need Help (Newbie)

Posted: Sun Jul 15, 2007 7:27 am
by garryhs
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is complex for me, but not most of you I guess.

I have a php script, which I need to POST a set of values (not array, not xml) to another script on more then one different URL's.  This code needs to be inserted in the middle section of my existing php code.

Code: Select all

<?php

#
# Existing PHP coding starts here...
#
blah blah blah
#
# Existing PHP coding ends here...
#

$value1 = "test value 1";
$value2 = "test value 2";
$value3 = "test value 3";
$value4 = "test value 4";
$value5 = "test value 5";

# Insert post here
# send values to http://www.url1.com/script.aspx -> No response required

# Insert post here
# send values to http://www.url2.com/script.php -> No response required


#
# Existing PHP coding starts here...
#
blah blah blah
#
# Existing PHP coding ends here...
#
?>
--------------------------------------------------------------------------------------------------
If I was doing this via a HTML Page, it would be...

Code: Select all

<?php

$value1 = "test value 1";
$value2 = "test value 2";
$value3 = "test value 3";
$value4 = "test value 4";
$value5 = "test value 5";

?>
<html>
 <body onLoad="document.process.submit();">
  <form action="http://www.url1.com/script.aspx" method=POST name=process>
   <input type=hidden name=value1 value="<?php echo $value1; ?>">
   <input type=hidden name=value2 value="<?php echo $value2; ?>">
   <input type=hidden name=value3 value="<?php echo $value3; ?>">
   <input type=hidden name=value4 value="<?php echo $value4; ?>">
   <input type=hidden name=value5 value="<?php echo $value5; ?>">
  </form>
 </body>
</html>

<?php
?>
--------------------------------------------------------------------------------------------------
Problem with this script is it only covers 1 url, and outputs HTML to the browser.

ANY HELP Please.

I have looked at scripts using
See:
---------------------------------------------------------------------------------------------------

Code: Select all

<?php
  function do_post_request($url, $data, $optional_headers = null)
  {
     $params = array('http' => array(
                  'method' => 'POST',
                  'content' => $data
               ));
     if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
     }
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
     }
     return;
  }
---------------------------------------------------------------------------------------------------
Problem is, I am not sure how to format the data (ie: $value1, $value2 etc)

I have no control over the external scripts, and they are expecting data as it would have been passed by a form.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]2.[/b] Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.[/quote]

Posted: Sun Jul 15, 2007 12:28 pm
by ianhull
Does it have to be POST data?

Could you not use GET method?

If you could use GET you can use


Code: Select all



<?php 

$value1 = "test value 1"; 
$value2 = "test value 2"; 
$value3 = "test value 3"; 
$value4 = "test value 4"; 
$value5 = "test value 5";

get_file_contents('http://www.url1.com/page.aspx?value1='.$value1.'&value2='.$value2.'');

get_file_contents('http://www.url2.com/page.php?value1='.$value1.'&value2='.$value2.'');

?>


[/syntax]

Posted: Sun Jul 15, 2007 7:25 pm
by garryhs
This works fine, BUT, the values have spaces in them, so the parms get truncated.

I could go and replace the spaces, but then there is an issue on the other end of the POST / GET.

Posted: Sun Jul 15, 2007 7:37 pm
by feyd
rawurlencode() and/or urlencode() may be of interest.