can anyone tell me how to read up to 3 remote files and compile the results in a query string which would now be sent to a page by the calling script, using headers.
Let me explain:
page1.php
$rs_1 = result from remote page a;
$rs_2 = result from remote page b;
$rs_3 = result from remote page c;
header("Location: page2.php?r1=".$rs_1."&r2=".$rs_2."&r3=".$rs_3)
You may be able to use file_get_contents, then make sure you urlencode the data when you construct the redirection url
$rs_1 =file_get_contents($urlA);
$rs_2 =file_get_contents($urlB);
$rs_3 =file_get_contents($urlC);
header("Location: page2.php?".
"r1=".urlencode($rs_1).
"&r2=".urlencode($rs_2).
"&r3=".urlencode($rs_3));
Also note this URL should be kept under 2000 characters.
Want to send more than 2000 chars?
If you want to utilize more data than 2000 characters would allow, you will need to POST it. One technique here would be to send some HTML back to the client with a form containing your data, and have javascript automatically submit it when the page loads.
The form could have a default button which says "Click here to continue..." which your JS would change to "please wait...". Thus users without javascript would drive it manually.
In other words, something like this:
<html>
<head>
<title>Please wait...</title>
<script>
function sendform()
{
document.getElementById('go').value="Please wait...";
document.getElementById('autoform').submit();
}
</script>
</head>
<body onload="sendform()">
<form id="autoform" method="POST" action="targetscript.php">
<input type="hidden" name="r1" value="htmlencoded r1data here">
<input type="hidden" name="r2" value="htmlencoded r2data here">
<input type="hidden" name="r3" value="htmlencoded r3data here">
<input type="submit" name="go" id="go" value="Click here to continue">
</form>
</body>
</html>
file_get_contents certainly helps, but for remote scripting CURL is better options.
This works well even if allow_url_include = On (in your php.ini)
$target = "Location: page2.php?";
$urls = array(1=>'url-1', 2=>'url-2', 3=>'url-3');
foreach($urls as $key=>$url) {
// get URL contents
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$target .= "&r".$key."=".urlencode($output));
}
header("Location: ".$target);
You can use the file_get_contents function.
API documentation: http://no.php.net/manual/en/function.file-get-contents.php
$file_contents = file_get_contents("http://www.foo.com/bar.txt")
Do note that if the file contain multiple lines or very, very long lines you should contemplate using HTTP post instead of a long URL.
Related
im newbie how to create multi search box like this https://imgur.com/yPWUKAL ? my data is from json and im using php prog. my goal is get the value input by user then transfer to my function.
// create & initialize a curl session
$curl = curl_init();
$url = "data.json";
// set our url with curl_setopt()
curl_setopt($curl, CURLOPT_URL, $url);
// return the transfer as a string, also with setopt()
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_exec() executes the started curl session
// $data_curl contains the output string
$data_curl = curl_exec($curl);
// close curl resource to free up system resources
// (deletes the variable made by curl_init)
curl_close($curl);
$data_json = json_decode( $data_curl );
if( !empty( $data_json )){
// fetch data
foreach ($data_json as $data ){
$loc_data = $data->location->city;
if( $loc_data == "San Francisco" ){
echo $loc_data;
}
}
}
else {
echo "No Data!";
}
?>
<form id="form" action="#" method="POST">
<input type="text" placeholder="Keyword" />
<input type="text" placeholder="Location" />
<input type="text" placeholder="Distance" />
<input type="submit" value="Search">
</form>
You need to understand the request cycle and the difference between client and server side code. PHP is a server side language, all the PHP code executes on a server and the output (whatever you echo, whatever HTML you write) is then sent to the client to be rendered in their browser.
To get user input (such as your form) you need to start a new request, i.e. when the form is submitted it starts a new request to the server, only the request now contains the data from the form in the $_POST superglobal.
Firstly, you need to add names to the inputs, as the name you use will be the key in the $_POST array, and the corresponding value will be the users input. E.g.
<input type="text" placeholder="Keyword" name="keyword">
Then whatever the user enters after submitting the form will be in $_POST['keyword'].
Secondly, you need to tell the form where to submit to using the action value. In this instance, you probably want it to submit back to the same php file, or you could move your function to retrieve data into another php file and then have the code submit to there. Assuming that your file is called search.php and people go to https://my.website/search.php to see the search box you would have the following:
<form id="form" action="/search.php" method="POST">
<input type="text" name="keyword" placeholder="Keyword">
<input type="text" name="location" placeholder="Location">
<input type="text" name="distance" placeholder="Distance">
<button type="submit">Submit</button>
</form>
Thirdly, when your script runs you need to check to see if there is any user input or not. If the user is just landing on your search page and hasn't filled the form out yet then there won't be any input for you to get. You can do this with a simple if statement and check the value of $_SERVER['REQUEST_METHOD'] to check if the request was a POST or a GET. Initial loads of the page without the form submission will use a GET request, where as form submissions will use a POST request.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// User has filled out the form, we can get location specific data
// ... your curl to fetch the data
if (!empty($data_json)) {
foreach ($data_json as $data) {
if ($data->location->city == $_POST['location']) {
echo $data->location->city;
}
}
}
}
Here is my current code:
<?php
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent are hold in an array for example
$urls=array('http://www.site1.com','http://www.site2.com/');
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
?>
My PHP code works, but here's the line I'm having issues with:
$urls=array('http://www.site1.com','http://www.site2.com/');
Basically I just want to have a text-area on my website where users can enter a list of URLs (one per line) and then it Posts it using the PHP code above.
My code works when I just have the URLs built into the code like you can see above, but I just can't figure out how to make it work with a text-area... Any help would be appreciated.
You can do it like below (on the same php page):-
<form method = "POST">
<textarea name="urls"></textarea><!-- add a lable that please enter new line separated urls -->
<input type = "submit">
</form>
<?php
if(isset($_POST['urls'])){
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent as new-line separated string and explode it to an array
$urls=explode('\n',$_POST['urls']); //Or $urls=explode('\\n',$_POST['urls']);
// if not worked then
//$urls=explode(PHP_EOL,$_POST['urls']);
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
}
?>
Note:- you can separate the form and logic on two different pages. that's seay i think.
You can use text-area and then you have to then explode by \n or if not work then explode by PHP_EOL to the initial url string and rest the code is same as above
You can pass to the form value as in bellow box is. any tag in HTML form have name attribute, assign name for any textarea, input or select tag.
<form method="post" name="first_form">
<input type="text" name="url" value="" />
<textarea name="note"></textarea>
<input type="submit" value="Post" />
</form>
In PHP you can the pass the value using two methods, $_GET or $_POST.
assign the tag name which you give for the HTML tag ($_POST['note'] or $_POST['url']) and also you can make it like an array $_POST.
<?php
if(isset($_POST)){
// display the posted values from the HTML Form
echo $_POST['note'];
echo $_POST['url'];
}
?>
Your Code:
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent are hold in an array for example
$urls=array('http://www.site1.com','http://www.site2.com/');
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
I have the following pages (code fragments only)
Form.html
<form method="post" action="post.php">
<input type="text" name="text" placeholder="enter your custom text />
<input type="submit">
</form
post.php
....
some code here
....
header('Location: process.php');
process.php
on this page, the "text" input from form.html is needed.
My problem is now, how do i pass the input-post from the first page through process.php without loosing it?
i dont want to use a process.php?var=text_variable because my input can be a large html text, formated by the CKeditor plugin (a word-like text editor) and would result in something like this process.php?var=<html><table><td>customtext</td>......
How can i get this problem solved?
I would like to have a pure php solution and avoid js,jquery if that is possible.
If you don't want to use $_SESSION you can also make a form in the page and then send the data to the next page
<form method="POST" id="toprocess" action="process.php">
<input type="hidden" name="text" value="<?php echo $_POST["text"]; ?>" />
</form>
<script>
document.getElementById("toprocess").submit();
</script>
or you can the submit the form part to whatever results in moving to another page.
Having said that using the $_SESSION is the easiest way to do this.
Either use $_SESSION or include process.php with a predefined var calling the post.
$var = $_POST['postvar'];
include process.php;
Process.php has echo $var; or you can write a function into process.php to which you can pass var.
maybe the doc can help: http://php.net/manual/fr/httprequest.send.php
especially example #2:
$r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
$r->setOptions(array('cookies' => array('lang' => 'de')));
$r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
echo $r->send()->getBody();
} catch (HttpException $ex) {
echo $ex;
}
But I wouldn't use this heavy way where sessions are possible and much easier, see previous answer/comments.
This is ok for instance if you want to call a pre-existing script awaiting post-data, if you can't (or don't want to) modify the called script. Or if there's no possible session (cross-domain call for instance).
I'm having a problem when using file_get_contents combined with $_GET. For example, I'm trying to load the following page using file_get_contents:
https://bing.com/?q=how+to+tie+a+tie
If I were to load it like this, the page loads fine:
http://localhost/load1.php
<?
echo file_get_contents("https://bing.com/?q=how+to+tie+a+tie");
?>
However, when I load it like this, I'm having problems:
http://localhost/load2.php?url=https://bing.com/?q=how+to+tie+a+tie
<?
$enteredurl = $_GET["url"];
$page = file_get_contents($enteredurl);
echo $page;
?>
When I load using the second method, I get a blank page. Checking the page source returns nothing. When I echo $enteredurl I get "https://bing.com/?q=how to tie a tie". It seems that the "+" signs are gone.
Furthermore, loading http://localhost/load2.php?url=https://bing.com/?q=how works fine. The webpage shows up.
Anyone know what could be causing the problem?
Thanks!
UPDATE
Trying to use urlencode() to achieve this. I have a standard form with input and submit fields:
<form name="search" action="load2.php" method="post">
<input type="text" name="search" />
<input type="submit" value="Go!" />
</form>
Then to update load2.php URL:
<?
$enteredurl = $_GET["url"];
$search = urlencode($_POST["search"]);
if(!empty($search)) {
echo '<script type="text/javascript">window.location="load2.php?url=https://bing.com/?q='.$search.'";</script>';
}
?>
Somewhere here the code is broken. $enteredurl still returns the same value as before. (https://bing.com/?q=how to tie a tie)
You have to encode your parameters properly http://localhost/load2.php?url=https://bing.com/?q=how+to+tie+a+tie should be http://localhost/load2.php?urlhttps%3A%2F%2Fbing.com%2F%3Fq%3Dhow%2Bto%2Btie%2Ba%2Btie. you can use encodeURIComponent in JavaScript to do this or urlencode in php.
<?
$enteredurl = $_GET["url"];
$search = urlencode($_POST["search"]);
if(!empty($search)) {
$url = urlencode('https://bing.com/?q='.$search)
echo '<script type="text/javascript">window.location="load2.php?url='.$url.'";</script>';
}
?>
dear all,
I need to send parameters to a URL without using form in php and get value from that page.We can easily send parameters using form like this:
<html>
<form action="http://..../abc.php" method="get">
<input name="id" type="text" />
<input name="name" type="text"/>
<input type="submit" value="press" />
</form>
</html>
But i already have value like this
<?php
$id="123";
$name="blahblah";
?>
Now i need to send values to http://..../abc.php without using form.when the 2 value send to abc.php link then it's show a value OK.Now i have to collect the "OK" msg from abc.php and print on my current page.
i need to auto execute the code.when user enter into the page those value automatically send to a the url.So i can't use form or href. because form and href need extra one click.
Is their any kind heart who can help me to solve this issue?
You can pass values via GET using a hyperlink:
<a href='abc.php?id=123&name=blahblah' />
print_r($_GET) would then give you the values, or you can use $_GET['id'] etc in abc.php
Other approaches, depending on your needs, include using AJAX to POST/GET the request asynchronously, or using include/require to pull in abc.php if it only includes specific functioanlity.eg:
$id="123";
$name="blahblah";
require('abc.php');
You can do:
$id="123";
$name="blahblah";
echo "<a href = 'http://foo.com/abc.php?id=$id&name=$name'> link </a>";
<?php
$base = 'http://example.com/abc.php';
$id="123";
$name="blahblah";
$data = array(
'id' => $id,
'name' => $name,
);
$url = $base . '?' . http_build_query($data);
header("Location: $url");
exit;