Hi guys!
have anyone here pulled out doing a php script that will send post data to a website/url then retrieving whatever the contents is of that page on that same file? ajax or no ajax included, is that doable?
| SitePoint Sponsor |



Hi guys!
have anyone here pulled out doing a php script that will send post data to a website/url then retrieving whatever the contents is of that page on that same file? ajax or no ajax included, is that doable?
If you won't dress like the
Victoria Secret girls,
don't expect us to act like soap opera guys.
Sure, look into cURL.![]()
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Afraid not, cURL is not that bad...I'm sure you'll do just fine.
Have a go at it, if you have any problems, just post back.![]()
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

Agreed, cURL is the way to go, also, do yourself a flavour and install LiveHttpHeaders addin for firefox and watch carefully what legal headers do when trying to work out errors and bugs - especially if your target site redirects stuff around its own system.
Here is a simple cURL wrapper you can use...
PHP Code:function curl_wrapper($url, $urlmethod, $fields)
{
/*
$urlmethod, if it
1 = get
2 = post
$fields is an associatei array
$login['email'] = 'me@this.com';
$login['pw'] = 'secret';
*/
//set POST variables
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
if ($urlmethod == 2)
{
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
}
if ($urlmethod == 1)
{
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url . '?'. $fields_string);
}
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}


$ar['key']='value';
foreach($ar as $key=>$value)
{
$postvars.=$key. '=' . urlencode($value) . '&';
$posted_data[$key]=$value;
}
$postvars=trim($postvars,'&');
$errstr=$errno='';
$fp= @ fsockopen('www.example.com',80,$errno,$errstr,30);
if(!$fp)
{
$posted_data['error']="fsockopen error no. $errno: $errstr";
log_results(0);
die();
}
@ fputs($fp, "POST /example.php HTTP/1.1\r\n");
@ fputs($fp, "Host: $host\r\n");
@ fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
@ fputs($fp, "Content-length: ".strlen($postvars)."\r\n");
@ fputs($fp, "Connection: close\r\n\r\n");
@ fputs($fp, $postvars . "\r\n\r\n");
$str='';
while(!feof($fp))
$str.=@ fgets($fp,1024);
@ fclose($fp);
echo $str;
Taken from some script don't remember.



Of course!
Here's an untested example, something like this should work.
PHP Code:$data = array('username' => 'joe', 'password' => 'secret');
$data = http_build_query($data);
$context = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context);
$fp = fopen('URL', 'r', false, $context);
// do something with $fp
Brad Hanson, Web Applications & Scalability Specialist
► Is your website outgrowing its current hosting solution?
► PM me for a FREE scalability consult!
► USA Based: Available by Phone, Skype, AIM, and E-mail.



To bhanson and fossil
I edited both your code and manage it to run somehow, but it doesn't seem to work the way i want it too. Please look at the page I'm trying to open. Now the data I'm passing are values i copied from firebug when i submitted the form on the original page.
the code works, but it doesn't seem to trigger whatever variable that makes the original url fire the script. you'll get what i mean when you visit the website and sending their form versus the output of the edited script.
CURL code from fossil
Typical PHP from bhansonPHP Code:<?php
function curl_wrapper($url, $urlmethod, $fields) {
$fields_string = http_build_query($fields);
$ch = curl_init();
if ($urlmethod == 'POST') {
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
}
elseif ($urlmethod == 'GET') {
curl_setopt($ch,CURLOPT_URL,$url . '?'. $fields_string);
}
return $result = curl_exec($ch);
curl_close($ch);
}
$fields = array('currency' => array ('GBP','JPY','CHF','CAD','AFN','ALL','DZD','ADF','ADP','AOA','AON','ARS','AMD','AWG','AUD',
'ATS','AZM','AZN','BSD','BHD','BDT','BBD','BYR','BEF','BZD','BMD','BTN','BOB','BAM','BWP',
'BRL','BND','BGN','BIF','XOF','XAF','XPF','KHR','CVE','KYD','CLP','CNY','COP','KMF','CDF',
'CRC','HRK','CUC','CUP','CYP','CZK','DKK','DJF','DOP','NLG','XEU','XCD','ECS','EGP','SVC',
'EEK','ETB','EUR','FKP','FJD','FIM','FRF','GMD','GEL','DEM','GHC','GHS','GIP','XAU','GRD',
'GTQ','GNF','GYD','HTG','HNL','HKD','HUF','ISK','INR','IDR','IRR','IQD','IEP','ILS','ITL',
'JMD','JOD','KZT','KES','KWD','KGS','LAK','LVL','LBP','LSL','LRD','LYD','LTL','LUF','MOP',
'MKD','MGA','MGF','MWK','MYR','MVR','MTL','MRO','MUR','MXN','MDL','MNT','MAD','MZM','MZN',
'MMK','ANG','NAD','NPR','NZD','NIO','NGN','KPW','NOK','OMR','PKR','XPD','PAB','PGK','PYG',
'PEN','PHP','XPT','PLN','PTE','QAR','ROL','RON','RUB','RWF','WST','STD','SAR','RSD','SCR',
'SLL','XAG','SGD','SKK','SIT','SBD','SOS','ZAR','KRW','ESP','LKR','SHP','SDD','SDP','SDG',
'SRD','SRG','SZL','SEK','SYP','TWD','TJS','TZS','THB','TOP','TTD','TND','TRL','TRY','TMM',
'USD','UGX','UAH','UYU','AED','UZS','VUV','VEB','VEF','VND','YER','YUN','ZMK','ZWD'),
'date' => '01/23/09', 'date_fmt' => 'us', 'dest' => 'Get+Table', 'exch' => 'USD',
'format' => 'HTML', 'lang' => 'en', 'result' => '1', 'value' => '1');
$data = curl_wrapper('http://www.oanda.com/convert/fxdaily', 'POST', $fields);
echo $data;
?>
Now don't look at me why i chose that website to practice this cURL/fopen, i was kinda dared if i could pull it off, and since im not busy and i could use to learn new tricks, I'm hacking it sort of.PHP Code:<?php
$data = array('currency' => array ('GBP','JPY','CHF','CAD','AFN','ALL','DZD','ADF','ADP','AOA','AON','ARS','AMD','AWG','AUD',
'ATS','AZM','AZN','BSD','BHD','BDT','BBD','BYR','BEF','BZD','BMD','BTN','BOB','BAM','BWP',
'BRL','BND','BGN','BIF','XOF','XAF','XPF','KHR','CVE','KYD','CLP','CNY','COP','KMF','CDF',
'CRC','HRK','CUC','CUP','CYP','CZK','DKK','DJF','DOP','NLG','XEU','XCD','ECS','EGP','SVC',
'EEK','ETB','EUR','FKP','FJD','FIM','FRF','GMD','GEL','DEM','GHC','GHS','GIP','XAU','GRD',
'GTQ','GNF','GYD','HTG','HNL','HKD','HUF','ISK','INR','IDR','IRR','IQD','IEP','ILS','ITL',
'JMD','JOD','KZT','KES','KWD','KGS','LAK','LVL','LBP','LSL','LRD','LYD','LTL','LUF','MOP',
'MKD','MGA','MGF','MWK','MYR','MVR','MTL','MRO','MUR','MXN','MDL','MNT','MAD','MZM','MZN',
'MMK','ANG','NAD','NPR','NZD','NIO','NGN','KPW','NOK','OMR','PKR','XPD','PAB','PGK','PYG',
'PEN','PHP','XPT','PLN','PTE','QAR','ROL','RON','RUB','RWF','WST','STD','SAR','RSD','SCR',
'SLL','XAG','SGD','SKK','SIT','SBD','SOS','ZAR','KRW','ESP','LKR','SHP','SDD','SDP','SDG',
'SRD','SRG','SZL','SEK','SYP','TWD','TJS','TZS','THB','TOP','TTD','TND','TRL','TRY','TMM',
'USD','UGX','UAH','UYU','AED','UZS','VUV','VEB','VEF','VND','YER','YUN','ZMK','ZWD'),
'date' => '01/23/09', 'date_fmt' => 'us', 'dest' => 'Get+Table', 'exch' => 'USD', 'format' => 'HTML',
'lang' => 'en', 'result' => '1', 'value' => '1');
$data = http_build_query($data);
$context = array( 'http' => array( 'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n".
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context);
$fp = fopen('http://www.oanda.com/convert/fxdaily', 'r', false, $context);
$contents = '';
while (!feof($fp)) {
$contents .= fread($fp, 8192);
}
echo $contents;
fclose($fp);
?>
If you won't dress like the
Victoria Secret girls,
don't expect us to act like soap opera guys.
That page supports a GET request, hese a quick example.
You may get to avoid cURL after all!PHP Code:<?php
$sBaseCurrency = 'GBP';
$aRequestedRates = array(
'DEM',
'FRF',
'ITL',
'CHF',
'JPY',
'CNY',
'RUB'
);
$sRawData = file_get_contents(sprintf(
'http://www.oanda.com/convert/fxdaily?redirected=1&exch=%s&format=HTML&sel_list=%s',
$sBaseCurrency,
implode('_', $aRequestedRates)
));
echo $sRawData;
?>![]()
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.



Yes i noticed that, but since it wasn't as hard as you said, i was in the mood to make the post version work. i already saw the get version and labeled it "last resort" if i cant make cURL work.
But thanks silverBulletUK for the code sample. Any idea why the post version doesn't work? I'm passing all the variables passed via POST as shown by firebug, but why isn't their script being triggered?
If you won't dress like the
Victoria Secret girls,
don't expect us to act like soap opera guys.

You've hit on one of the main problems involved with depending upon processing on a remote server, via http protocols, dealing with the myriad of things that can go wrong.
cURL is well tooled up to deal with these situations, hence it was recommended you use it.
Like everything useful, it has a learning curve which you will have to climb.
Edit:
I should have added "specifically when using the POST method ..."
Last edited by Cups; Jan 26, 2009 at 10:07.
Bookmarks