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);
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;
}
}
}
}
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have an html forum with 3 fields Naam Achternaam Email
After the user fills in the forum and clicks submit, it wil call a php script called action_page.php.
Taht script should be posting back to the screen the given information form the html forum.
At this point it only gives back 3 NULL values
Naam:
Achternaam:
Email:
This is the html code
how can i get the PHP script to take the filled in fields from the html page and show then on screen.
<?php
// what post fields?
$fields = array(
'Naam' => $Naam,
'Achternaam' => $Achternaam,
'Email' => $Email,
);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$ch = curl_init();
// 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, $postvars);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
?>
This is the php code
The html page has 3 fields
<html>
<body>
<form action="/action_page.php" method="post">
Naam: <input type="text"name="Namm"><br>
Achternaam: <input type="text" name="Achternaam"><br>
Email: <input type="text" name="Email"><br>
<td><input type='submit' name='post' value='post'></td>
<td><input type='reset' name='Rest' value='Reset'></td>
</form>
</body>
</html>
but after i click op sumbit the action_page returns a blank page or for each filled field it echo NULL on the screen.
What i am trying to make is an php page that prints the 3 filled field from the html page that should post them to the php page so that can print them to the screen.
am i dooing something wrong here or mistyped in the code some where?
Not sure if this is what you are looking for, but to get posted values, you'll use the $_POST superglobal
$fields = array(
'Naam' => $_POST["Naam"],
'Achternaam' => $_POST["Achternaam"],
'Email' => $_POST["Email"],
);
Say we have page 1:
<?php
$text=$_POST['text'];
echo 'You wrote "' . $text . '".';
?>
and page 2, which takes the user input, POSTs it to Page1, and gets the page (You wrote (input).). I'm not really sure about how to do this: googling (or better, stackoverflowing) a while, I found this question, which explains how to POST a variable to another page, but not how to get the code of the page. So, how to do both?
EDIT: An example of the actual situation.
The user inputs the number 245. I get the number, pass it to this external page, get the page, retrieve the result (5*7*7) and show it.
In italics you see the part I need.
I'd suggest using curl to POST the data, and then you'll probably need to parse the returned HTML.
Here's a blog post about how to use cURL to post. I'll leave using google to find an example of parsing HTML with PHP as an exercise for the reader . . .
page 1:
<form method="post" action="page2">
<input name="input" type="text"/>
<input type="submit" value="sendtopage2"/>
</form>
page 2:
<?php
$text=$_POST['input'];
echo 'You wrote "' . $text . '".';
?>
Working example with cURL:
$function="sin(x)";
$var="x";
$url='http://www.numberempire.com/integralcalculator.php';
echo '<base href="' . $url . '">'; // Fixes broken relative links
$Curl_Session = curl_init($url);
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "function=$function&var=$var");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 0);
$content=curl_exec($Curl_Session);
curl_close ($Curl_Session);
I need help in trying to use curl to post data to a page and retrieve the results after the form has been submitted.
I created a simple form:
<form name="test" method="post" action="form.php">
<input type="text" name="name" size="40" />
<input type="text" name="comment" size="40" />
<input type="submit" value="submit" />
</form>
In addition, I have php code to handle this form in the same page. All it does is echo back the form values.
The curl that I have been using is this:
$h = curl_init();
curl_setopt($h, CURLOPT_URL, "path/to/form.php");
curl_setopt($h, CURLOPT_POST, true);
curl_setopt($h, CURLOPT_POSTFIELDS, array(
'name' => 'yes',
'comment' => 'no'
));
curl_setopt($h, CURLOPT_HEADER, false);
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($h);
echo $result;
When I launch the page with the curl code in it, I get the form.php page contents but it doesn't not show
the variables that PHP should have echo'd when the form is submitted.
would appreciate any help with this.
Thanks.
AHHHH after your comment, the problem is clear.
you need to include one more POST variable in your array.
'submitted' => 'submitted'
the submit button also returns a post value if clicked, which you are checking in your form processing PHP.
if ($_POST['submitted']) {
in your curl code however you have left out the 'submitted' post variable.
if this is what you are looking for, please select the check mark next to this answer. thanks!
From reading the other two answers, and the comment by the OP, I have a couple of ideas.
Specifically, the OP Comment was:
Submitting the form manually does produce the right output. The PHP that handles the form is: if(isset($_POST['submitted'])){echo $_POST[name] ..... etc. and that's it
Under standard conditions, your basic form, as noted in the Original Question, would generate a $_POST array like the following:
array(
'name' => 'The Name as Entered in the Form' ,
'comment' => 'The Comment as Entered in the Form' ,
'submit' => 'submit' # From the "Submit" button
);
Your comment suggests that some aspect of your form handler is looking for a $_POST element called "submitted".
1) The basic form, as set out in the question, will always return FALSE for a check for $_POST['submitted'], and, as such, will trigger the ELSE action (if present) for that conditional.
2) Your CURL Action does not set either 'submit' or 'submitted', and, again, will always return FALSE for the conditional.
So, I would suggest that you:
1) Check your Form Handler, and see what fields are essential, what their names are and what their content should be.
2) Check your Basic Form, and ensure that it has the appropriate fields.
3) Check your CURL Action, and ensure that it details every field which is required. (Easy check would be to insert print_r( $_POST );die(); at the top of your Form Handler and submit the basic form. This will show you exactly what the form is sending, so you can recreate it in CURL.
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.