I'm creating an system that uses online compiler. IDEONE give me this feature (throgh an Web Service), but with a price for an high volume of compilations.
Then I'm trying to use codepad, but it doesn't have an Web Service... codepad has an initial page, and clicking it's submit button, apparently the same page loads (the form's action is "/")...
I'm using curl to load page, but I'm getting "Internal Server Error". This is my code: pastebin Code, I'm using 000webhost, I don't know if i did something wrong or if my webserver doesn't support it.
Have you trued removing the TIMEOUT? or maybe extending it?
Try this maybe:
<html>
<div align="center">
<form action="compilador.php" method="POST">
<textarea id="source" name="source"></textarea>
<input type="submit" value="Enviar" />
<?php
if(isset($_POST['source']) && $_POST['source'] != "")
{
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL, "http://codepad.org");
/**
* Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post_data=$_POST;
$post_data['lang'] = 'C';
$post_data['private'] = True;
$post_data['run'] = True;
foreach($post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec ($ch);
/**
* Close cURL session
*/
curl_close($ch);
echo "<br /><br />RESULT: {".$result."}";
}
?>
</form>
</div>
</html>
Related
I have a simple form:
<form action="" method="POST">
<input type="text" name="arg1"/>
<input type="text" name="arg2"/>
<input type="submit"/>
</form>
Then when it's submitted, I POST arg1 via cURL and update its value based on the cURL response data. I then call another function to change arg2 using a call to a Java program with exec.
And these two updated arg1 and arg2 should be the value attributes for that form after I submit it initially. (So that after the first submit, I just need to click "submit" as long as I keep getting data from the cURL request)
I tried using global as seen in this post: Giving my function access to outside variable ,but it doesn't work for me
My code is something like that:
<form action="" method="POST">
<input type="text" name="arg1" value="<?php echo $arg1;?>"/>
<input type="text" name="arg2" value="<?php echo $arg2;?>"/>
<input type="submit"/>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$arg1_post = $_POST["arg1"];
$arg2_post = $_POST["arg2"];
$curl_post_fields = '{"arg1":' . $arg1_post . ', "arg2": "'. $arg2_post . '"}';
sendCurlPost($curl_post_fields);
}
function sendCurlPost($curl_post_fields){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url/" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
if ($response=curl_exec($ch)){
global arg1;
$arg1 = // some value from the $response
$output=null;
$retval=null;
runJavaProgram();
}
}
function runJavaProgram (){
exec('C:\Java\jdk-18\bin\java.exe Main.java 2>&1 ', $output, $retval);
global $arg2;
$arg2 = $output[0];
I keep getting Undefined variable even after the first POST. I also tried other methods using SESSION and COOKIE but that didn't work either
I'm unable to scrape data from few websites using curls.
I'm using CURL to scrape website from url's. It works great in 80% of the urls I use. But some url's don't seem "scrapeable". For example, when I try to scrape https://www.nextdoorhub.com/ and https://www.atknsn.com/, it doesn't work. the website keeps showing blanks and at the end it doesn't return a result.
This is my code:
<center>
<br/>
<form method="post" name="scrap_form" id="scrap_form" action="scrape_data.php">
<b>Enter Website URL To Scrape Data:</b>
<input type="input" name="website_url" id="website_url">
<input type="submit" name="submit" value="Submit" >
</form>
</center>
<?php
error_reporting(E_ALL ^ E_NOTICE );
$website_url = $_POST['website_url'];
$result = scrapeWebsiteData($website_url);
function scrapeWebsiteData($website_url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $website_url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_BINARYTRANSFER,1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$regextit = '<div id="case_textlist">(.*?)<\/div>/s';
preg_match_all($regextit, $result, $list);
/* echo "<pre>";
print_r($list[1]); die; */
$regex = '/[\'" >\t^]([^\'" \n\r\t]+\.(jpe?g|bmp|gif|png))[\'" <\n\r\t]/i';
preg_match_all($regex, $result, $url_matches);
$count = count($url_matches[1]);
// set the local path of image
$local_path = 'C:\udeytech\htdocs\tests\images\\';
for($i=0; $i<$count; $i++)
{
preg_match_all('!.*?/!', $url_matches[1][$i], $matches);
$last_part = end($matches[0]);
////match image name last part of anything .jpg|jpeg|gif|png
preg_match("!$last_part(.*?.(jpg|jpeg|gif|png))!", $url_matches[1][$i], $matche);
$secons_part = $matche[0];
$info = pathinfo($secons_part);
$image_name = $info['basename'];
//save image url in a variable
$image_url = $url_matches[1][$i];
$image_path = scrapeWebsiteData($image_url);
$file_open = fopen($local_path.$image_name, 'w');
fwrite($file_open, $image_path);
fclose($file_open);
}
?>
Have you tried to load either of these sites in your browser and look at the responses?
nextdoorhub is using angular and atknsn looks to be heavy on jQuery. Long story short, these sites need to run javascript to render the full HTML you're intending to scrape.
Using PHP + cURL alone won't cut it. Look at threads that discuss scraping angular and that will point you in the right direction. (Hint: you need to scrape these sites with node.js)
I have a page with a recaptcha in it, and it had been running without any problem for two months. But now, since a few days, it has been acting weird. I have tried many several times, but the captcha is simply not working, the verification part.
Here is the code
$captcharesponse = test_input($_POST["g-recaptcha-response"]);
$status = captcha($captcharesponse);
...
function captcha($t){
$captcharesponse = $t;
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($curl, CURLOPT_POSTFIELDS, 'secret=7...PRIVATE_KEY...S&response=' . $captcharesponse);
$result = json_decode(curl_exec($curl), true);
curl_close($curl);
if($result['success'] == false){
error_log(date("Y-M-d, D h:i:s A") . " : Result = " . $result['success'] . ", and error = " . $result['error-codes']);
}
return $result['success'];
}
And no matter what, even if I am not even entering the captcha, still the page is taking too long, and hence nothing is working. Please not that other things are simply skipped if the captcha is wrong, so there is no way that other things are causing the delay.
Thanks in advance
PS. I am not using any kind or library or anything, and it did use to work some time back without any problem.
The 'test_input()' code:
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
The problem has been resolved,
Apparently it was a problem on reCAPTCHA's end. The above provided code is now working flawlessly, and all slow-performance problems have been resolved as well.
Thank you all.
PS. Others can use this code if the want.
I'd say to use the recaptcha library, available at:
https://github.com/google/recaptcha
First of all, download the files, most important is recaptchalib.php (you can download all files clicking the download zip button at right).
Then unzip it to your folder and use it like the example unzipped along (example-recaptcha.php):
<?php
require_once "recaptchalib.php";
// Register API keys at https://www.google.com/recaptcha/admin
$siteKey = "YOURSITEKEY";
$secret = "YOURSECRET";
$lang = "en";
$resp = null; // The response from reCAPTCHA
$error = null; // The error code from reCAPTCHA, if any
$reCaptcha = new ReCaptcha($secret);
if ($_POST["g-recaptcha-response"]) { // Was there a reCAPTCHA response?
$resp = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
?>
<html>
<head><title>reCAPTCHA Example</title></head>
<body>
<?php
if ($resp != null && $resp->success) {
echo "You got it!";
}
?>
<form action="" method="post">
<div class="g-recaptcha" data-sitekey="<?php echo $siteKey;?>"></div>
<script type="text/javascript"
src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang;?>">
</script>
<br/>
<input type="submit" value="test recaptcha" />
</form>
</body>
</html>
i send data to server from pc local through this form :
<form method="post" action="proses.php">
<input type="hidden" name="id"><br>
Tanggal <input type="text" name="tgl"><br>
Pesan <textarea name="isi" cols="29" rows="5"></textarea> <br>
Nomor Hp <input type="text" name="nope"><br>
<input type="submit" name="submit" value="Submit"> </form>
and this is code proses.php
<?php
$id = $_POST['id'];
$tgl = $_POST['tgl'];
$isi = $_POST['isi']; $nope= $_POST['nope'];
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, 'http://adibiken.com/SEM/kir.php');
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, "id=".$id."&tgl=".$tgl."&isi=".$isi."&nope=".$nope);
curl_setopt($curlHandle, CURLOPT_HEADER, 0);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
curl_setopt($curlHandle, CURLOPT_POST, 1);
curl_exec($curlHandle);
curl_close($curlHandle); ?>
this is code for kir.php ( in server )
<?php $id = $_POST['id']; $tgl = $_POST['tgl']; $isi = $_POST['isi']; $nope = $_POST['nope'];
$quer = "INSERT INTO `adibiken_sem`.`inbox` (`id`,`tgl` ,`isi` ,`nope`)VALUES ( '$id','$tgl', '$isi', '$nope')";
mysql_query($quer); ?>
PROBLEM : data are have been successful SENDING with EMPTY RECORD IN DATBASE SERVER...
need help please
Don't know if this is the problem but for sure
VALUES ( '$id','$tgl', '$isi', '$nope')"
should be
VALUES ( '".$id."','".$tgl."', '".$isi."', '".$nope."')"
In file "proses.php" try using "&" instead of "&". Also in file kir.php ( in server ) run print_r($_POST) or log the whole array somewhere to chceck if you are recieving the correct variable names.
use the http_build_query(...) to form postfields from array.
$request = http_build_query($_POST);
...
curl_setopt($curlHandle, CURLOPT_POSTFIELDS,$request)
If you getting correct values in $_POST array then something is wrong with your SQL syntax. You can try to execute your query in phpMyAdmin or something else and debug it.
It might happen that MySQL fires an error, but you could not see it. You can get any output from kir.php including error messages (it is enabled on your server):
$curResponse = curl_exec($curlHandle);
echo $curResponse;
i have no idea how to solve a problem with sending $_POST. I want to fill a form at example.com
//at example.com
<form action="foo.php" method="post" >
<input name="bar1" type="text" />
<input name="bar2" type="text" />
<input name="bar3" type="text" />
<input value="Send" type="submit" />
</form>
and then it goes to foo.php :
<?php //foo.php
echo 'added: <p>'.$_POST['bar1'].'<br />'.$_POST['bar2'].'<br />'.$_POST['bar3'];
?>
and in the same time it also send
$_POST['bar1'], $_POST['bar2'], $_POST['bar3']
to exampledomain.com/foobar.php where it can be saved to a file - that's not a problem.
I don't know how to send info to both php scripts at once - one is external one. I guess i have to send it somehow inside foo.php
There is kind of solution - redirecting to exampledomain.com/foobar.php inside foo.php but it isn't acceptable in my case - I want to do it without making user exit example.com
Thanks in advance and hope you can undestand my problem - if not just ask a comment
EDIT: Based on Pete Herbert Penito's answer:
<?php //inside foo.php
$url = 'http://exampledomain.com/foobar.php';
$fields_string='';
foreach($_POST as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//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($_POST));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
I would use CURL to construct a post request:
<?php
// these variables would need to be changed to be your variables
// alternatively you could send the entire post constructed using a foreach
if(isset($_POST['Name'])) $Name = $_POST['Name'];
if(isset($_POST['Email'])) $Email = $_POST['Email'];
if(isset($_POST['Message'])) $Message= htmlentities($_POST['Message']);
$Curl_Session = curl_init('http://www.site.com/cgi-bin/waiting.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "Name=$Name&Email=$Email&Message=$Message");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
?>
From Link:
http://www.askapache.com/php/sending-post-form-data-php-curl.html
In your foo.php:
<?php
include 'http://exampledomain.com/foobar.php';
Note: you need to enable allow_url_fopen in your php.ini file.
You will need to do one of the POST's with javascript ajax.
Then the real post which will redirect the browser like normal.
http://www.w3schools.com/jquery/ajax_post.asp
$(selector).post(url,data,success(response,status,xhr),dataType)