I'm trying to search a user using the Github api, but I'm not sure what I'm doing wrong here.
My html form:
<form action="" method="get">
<div class="form-group">
<label >Buscar Usuario</label>
<input class="form-control" type="text" name="username">
</div>
<button type="submit" name="submit" class="btn btn-default">Procurar</button>
</form>
My php code:
<?php
function buscaUser()
{
$user= $_GET['username'];
$ch = curl_init();
$url ="https://api.github.com/search/users?q=".$user;
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
}
?>
YOu didn't call the buscaUser() anywhere
Do it this way:
<?php
function buscaUser($user) {
$ch = curl_init();
$url ="https://api.github.com/search/users?q=".$user;
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
}
?>
Then you can call the function and pass the submitted value:
<?php
echo buscaUser($_GET['username']);
?>
Should do!
Related
I have watched some tutorials on how to pass a variable from a form to a php file, and cannot get anything to happen when I click the submit button. I am making a page that searches the Clash of Clans API to display the clan searched.
I just need to have the form submit the clan tag to the php script.
Here is my code. The file is a php file and the form action is blank because the php is in the file.
<?php
header('Content-Type: text/html; charset=UTF-8');
if (isset($_POST['submit'])) {
$clantag = $_POST['var'];
}
$token = Token Goes Here;
$url = "https://api.clashofclans.com/v1/clans/" . urlencode($clantag);
$ch = curl_init($url);
$headr = array();
$headr[] = "Accept: application/json";
$headr[] = "Authorization: Bearer ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$data = json_decode($res, true);
curl_close($ch);
echo "<pre>";
var_dump($data);
echo "</pre>";
?>
<form action="" method="post">
<input id="clan-tag" type="text" name="var" value="" />
<input id="clanSearchBtn" type="button" name="submit" value="Submit">
</form>
You should replace the type of the button from "button" to "submit"
<form action="" method="post">
<input id="clan-tag" type="text" name="var" value="" />
<input id="clanSearchBtn" type="submit" name="submit" value="Submit">
</form>
So im basically trying to call an API that is supposed to ping a given IP address, but everything ive been trying hasn't been working, they used to work in the past because these are just snippets i gathered from an old project that used to work just fine, now they dont work, ive checked to see if it logged any PHP errors, but there was nothing in the logs.
Here are my sample codes:
cURL:
<?php
if (Isset($_POST['submit'])){
$host = $_POST['host'];
$url = "http://someapi.com/ping/?host=".$host;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}
?>
<input type="text" name="host" placeholder="IP" />
<input type="submit" name="submit" value="Geo Locate" />
<br>
<center>
<?php echo $data; ?>
</center>
</form>
#File_Get_Contents
<form action="" method="POST">
<?php
If(Isset($_POST['submit'])){
$host = $_POST['host'];
$api = #File_Get_Contents("http://someapi.com/ping/?host=" .$host. "");
echo $api;
}
?>
<input type="text" name="host" placeholder="IP" />
<input type="submit" name="submit" value="Geo Locate" />
<br>
<center>
<?php echo $api; ?>
</center>
</form>
could it be something wrong with my hosting service maybe?
I try to get news from website
http://www.science-support.ru/news.html
, but I can't extract anything from web page content.
When I apply file_get_contents() I get string for which other function don't work. Initially, I try to use file_get_html() from simple_html_dom.php, but then other functions from this source don't recognize any elements in obtained DOM.
About other function:
<?php
$content = file_get_contents('http://www.science-support.ru/news.html'); //normal page
$content = substr($content,20); //strange characters
$content_arr = explode( 'div id="box3"' , $content ); //doesn't work
echo $content;
echo $content_arr[0];
?>
After substr() I get something like
" <�/tr> <�/table> <�/div> <�div id="box3"><�!-- InstanceBeginEditable name="page-content" --> <�h4 class="yellow">14.11.2014 />2>AB8 $>=40/<�/h4> <�p>1JO2;O=K #57C;LB0BK :>=:C#A0 =0 ?#8AC645=85 ?>8A:>2KE 3#0=B>2 ?> #>3#0<<5 =0CG=>-B5E=8G5A:>9 <>45#=870F88 8 ?>2KH5=8N :20;8D8:0F88 <>;>4KE CG5=KE >AA88 2014-2015 33.<�a href="news/news2014-nota-res.html" class="txt" >?>4#>1=55<�/a><�/p>"
How can I extract readable content? Thanks
Try to do this from Curl i have post some code that will help you
<?php
if( isset( $_POST['site_url'] ) && !empty( $_POST['site_url'] ) ){
echo get_html($_POST['site_url']);
} else {
echo 'false';
}
function get_html($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
get website url from your html form this is html how to set
<div class="col-md-offset-2 col-md-8">
<form role="form" id="siteform" method="post">
<div class="form-group">
<input type="url" class="form-control" name="site_url" id="site_url" placeholder="Enter your site address">
<span class="help-block"></span>
</div>
<button data-loading-text="Please wait..." type="submit" id="url_getter" class="btn btn-default btn-success">Submit</button>
</form>
</div>
</div>
I know my curl script works correctly. I can hardcode the email and pass in curl and it works fine, but now I am trying to create a php script to input it and it doesn't work. I will post links and code below.
I am using the input email#ccc.com:passss.
http://cyber-hosted.com/CH/index.html
<html>
<head>
</head>
<body>
<form action="n.php" method="post">
<input type="text" name="mail />
<input type="password" name="pass" />
</form>
</body>
http://cyber-hosted.com/CH/n.php
<?php
$usermail = $_POST['Mail'];
$pass = $_POST['pass'];
$url = "https://secure.tesco.com/register/default.aspx?vstore=0";
//
$h = curl_init();
curl_setopt($h, CURLOPT_URL, $url);
curl_setopt($h, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($h, CURLOPT_POST, true);
curl_setopt($h, CURLOPT_POSTFIELDS, "form=fSignin&from=https%3A%2F%2Fsecure.tesco.com%2Fclubcard%2Fmyaccount%2Fhome.aspx&formData=bmV3UmVnPWZhbHNlJg%3D%3D&loginID=$usermail&password=$pass&seamlesswebtag=&confirm-signin.x=47&confirm-signin.y=18");
curl_setopt($h, CURLOPT_HEADER, true);
curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
curl_setopt($h, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($h, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($h, CURLOPT_FOLLOWLOCATION, true);
//
$result = curl_exec($h);
echo $result;
?>
You typed mail with uppercase:
$usermail = $_POST['Mail'];
// here -----^
Change it to:
$usermail = $_POST['mail'];
Ah and I almost forgot the double quotes here:
<input type="text" name="mail />
<!-- here --------^ --->
It should be <input type="text" name="mail" />
Try giving the fields a value attribute
<input type="text" name="mail" value=""/>
<input type="password" name="pass" value=""/>
Also you are missing the submit button from your supplied code
<input type="submit" name="submit" value="Submit"/>
And finally you should URL encode the usermail and password parameters in the cURL request.
I need some advice with this curl script. I am trying to send values from the FORM to the curl script, but seems that the curl script doesn`t receive the info.
Here is the script:
<?php
if($_SERVER['REQUEST_METHOD'] != 'POST') {
$self = $_SERVER['PHP_SELF'];
?>
<form method="post" action="<?php echo $self; ?>" class="form-horizontal">
<fieldset>
<legend>SMS Contact</legend>
<div class="control-group">
<label for="basic" class="control-label">Name and Surname</label>
<div class="controls">
<input type="text" name="name" id="name" disabled class='input-square' value="<?php print $contactname;?> <?php print $contactsurname;?>">
</div>
</div>
<div class="control-group">
<label for="basic" class="control-label">Mobile No</label>
<div class="controls">
<input type="text" name="mobile" id="mobile" disabled class='input-square' value="<?php echo urlencode($contactmobile);?>">
</div>
</div>
<div class="control-group">
<label for="textcounter" class="control-label" id="textcounter">Textarea</label>
<div class="controls">
<textarea name="textcounter" id="textcounter" class='input-square span9 counter' data-max="160" rows='6'></textarea>
</div>
</div>
<div class="form-actions">
<button class="btn btn-primary" type="submit">Send SMS</button>
</div>
</fieldset>
</form>
<?php
} else {
$mobile = $_POST['mobile'];
$text = $_POST['textcounter'];
$username = 'xxxx';
$password = 'xxxx';
// Set Timezone
date_default_timezone_set('Africa/Johannesburg');
$date = date("m/d/y G.i:s", time());
// Create Unique ID
$code = md5(time());
$newid = $code.$clientid;
$sql="INSERT INTO sent_itmes (sent_id, sent_message, sent_date, sent_quantity, client_id, sent_mobile)VALUES('$newid', '$text', '$date', '1', '$clientid', '$mobile')";
$result=mysql_query($sql);
$url = "http://bulksms.2way.co.za/eapi/submission/send_sms/2/2.0"; // URL to calc.cgi
$fields = array(
'site'=>'',
'username'=>($username),
'password'=>($password),
'message'=>urlencode($text),
'msisdn'=>urlencode($mobile)
);
$fields_string="?";
//url-ify the data for the POST
foreach($fields 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_RETURNTRANSFER, false);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
ob_start();
curl_exec($ch);
ob_end_clean();
echo '<div class="alert alert-block alert-success">
<a class="close" data-dismiss="alert" href="#">×</a>
<h4 class="alert-heading">Success!</h4>
Your message has been Sent!
</div><br/><br/>Search Contact';
//close connection
curl_close($ch);
}
?>
When submitting the form, I get the error from response server "No recipients specified", so this means that the form value "mobile" doesnt pass though the value to the curl.
Help please, going off my mind.
Try adding a header that describes the content you post and maybe content you apply
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/x-www-form-urlencoded',
'Accept: text/html',
'Expect: ',
) );
use this for sending data
//open connection
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,POSTVARS.$fields_string); // this line is edited..
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
//execute post
ob_start();
curl_exec($ch);
your order was wrong...
more info http://www.askapache.com/php/sending-post-form-data-php-curl.html
When you submit form with disabled field, that field do not sent:
<input type="text" name="mobile" id="mobile" disabled class='input-square' value="<?php echo urlencode($contactmobile);?>">
If you want to send "mobile", you should add hidden field for it. And maybe replace disabled field name