How do I use curl to display a table on a page? - php

I want to use curl to retrieve a table from an external page. So far my code retrieves all data from the page. I have read that using preg_match or preg_replace is the way to go about it.
This is my code so far:
<?php
$ch = curl_init() or die(curl_error());
curl_setopt($ch, CURLOPT_URL,"http://www.megaupload.com/?d=XE30L1GA&w=631&h=392");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1=curl_exec($ch) or die(curl_error());
echo "<font color=black face=verdana size=3>".$data1."</font>";
echo curl_error($ch);
curl_close($ch);
?>
This is the data I want to retrieve from the page:
<FORM method="POST" id="captchaform">
<INPUT type="hidden" name="captchacode" value="1a589e0a53c54f937eb8">
<INPUT type="hidden" name="megavar" value="bed55b1a7a26e4bb0aa11df4188f58bda6958d72856a637ca55cbda4c24c89b97d52e39e3a2d1d26082273dec61e68416bd929d566cf63cd8441e9f6166d667618060608c5d63c9341e72fc036ea4ed1ec8d099bc488fa1f3c5df7228e10a0f79c4e5fb78fe41f0d9873194c56f94dfbcc008e4a7c8ebc58c8169d42cb98a9a86d8cb6dfbae8791b9dfcf5a3119f7941cc07e9ba48934ad9b5f865cd32e71bf6d3a3954ddc4e75925aaf2c0ec6d1dd884ccc4d299be3e41957a967e68a143af49d6b7ff7b36dac725f20214e58feb32662e09f5840ac76cff67f7d99e67ffb485d72382dd20cece80e4307dfef720a51734d7f9a4a363de0950d3d0e927423642a79b2e68ed177d2ad2d877c1c2052969ec61737749b05864b3a74d241981d6b89">
<TR>
<TD>Enter this </TD>
<TD width="100" align="center" height="40"><img src="http://wwwq32.megaupload.com/gencap.php?23243f17c2404dd4.gif" border="0" alt=""></TD>
<TD> here:</TD>
<TD><input type="text" name="captcha" id="captchafield" maxlength="4" style="border:solid 1px; border-color:#C4C4C4; background-color:#F9F9F9; width:50px; height:25px;"></TD>
</TR>
</FORM>

preg_*() is certainly not the way to do it. Use a HTML parser.

Related

Question marks when execute arabic letters in php

I want to build simple page to get exam results from university server.
First I built a form that send student number to university server and take you to university page. Here is the code:
<form action="https://exam.albaath-univ.edu.sy/exam-med/re.php" method="post" accept-charset="UTF-8">
<p style="text-align: center;">
<input name="number1" type="hidden" value="" />
<input name="nospy" type="hidden" value="" />
</p>
<table>
<tbody>
<tr>
<td>choose field</td>
<td>
<select name="nospy">
<option value="1">Med</option>
</select>
</td>
</tr>
<tr>
<td>enter your number</td>
<td style="text-align: right;"><input name="number1" size="20" type="text" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="show result" /></td>
</tr>
</tbody>
</table>
</form>
This code works perfectly.. The next step I tried to do is to make the form redirect to another page on my website. This page contains php code that take the information the send it to university server then get the response and print it in same page without going to university page.
The code was like this:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$nospy = $_POST['nospy'];
$number1 = $_POST['number1'];
// send the information to another PHP page using cURL
$url = 'https://example.com/other_page.php';
$post_data = array(
'nospy' => $nospy,
'number1' => $number1
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// process the response from the other PHP page
// convert it to the Windows-1256 encoding
$response = iconv("UTF-8", "Windows-1256", $response);
// display the converted response in HTML
echo $response;
}
?>
This code also worked but the problem is that the results contains arabic names and php still give question marks instead of arabic letters.. I tried many solutions like converting text to utf-8 and window-1256 encoding... it doesnt work.. can anyone help
I need the results to support arabic letters
please try adding the following to the header to the returned output.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

I use POST method for form but echo $_SERVER['REQUEST_METHOD'] result is GET

i have a problem that is i use POST method to post my form but when i echo $_SERVER['REQUEST_METHOD'] the result is GET, why?
this is my code
<div align="center">
<form action="../application/components/check.php" method="POST" >
<table align='center' cellspacing='20px;' class='menubg'>
<tr>
<td colspan='2'>
<div style='height:30px;'></div>
</td>
</tr>
<tr>
<td align='right' style='color:#ffffff; vertical-align:middle; font-family:arial; font-size:13px;'>User Name</td>
<td align='left'> <input type="text" name="uname" value="" onfocus="this.select()"/> </td>
</tr>
<tr>
<td align='right' style='color:#ffffff; vertical-align:middle; font-family:arial; font-size:13px;'>Password</td >
<td align='left'>
<input type="password" name="pass" value="" onfocus="this.select()"/> </td>
</tr>
<tr>
<td colspan='2' align='right'>
<input type='submit' value='ENTER'/>
</td>
</tr>
</table>
</form>
On check.php, i write like
if($_SERVER['REQUEST_METHOD'] == "POST") {
//code
}
it wont go in that, after that i try
echo $_SERVER['REQUEST_METHOD'] ;
the result return GET.
I have try another pc to run this code, it ok, but come to my pc then cannot,
my pc using xampp,so any different, and how to solve it??
Try if($_POST) to check if it's a POST. Since all pages have $_GET set, if($_GET) will not work. You can also try something like if(count($_GET)>0). That will tell you if there is any $_GET data.
That's because $_SERVER['REQUEST_METHOD'] contains the request method i.e. what method was used to access the page (which will be GET in nearly all cases where you're just requesting a webpage from a browser), NOT any POST or GET data that was submitted from a form. POST data that was submitted from a form should be interfaced with via $_POST.

Amazon remote log-in (PHP cURL) | Cookies

I'm trying to make a remote log-in for Amazon. So basically, an user will log-in through our local form. And if everything he filled in is correct, he will be logged in at Amazon.
`
if(isSet($_POST['submit']))
{
$account = $_POST["account"];
$pass = $_POST["pass"];
$ch = curl_init("https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_ya_signin");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIESESSION, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, "ap_email=" .$account."&ap_password=".$pass."");
curl_exec($ch);
if (curl_errno($ch))
{
print curl_error($ch);
}
else
{
curl_close($ch);
}
}
else
{
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="login">
<table width="100%">
<tr align="center">
<td width="50%" align="right"><font color="navy">E-Mail</font></td>
<td width="50%" align="left"><input type="text" name="account" size="10"></td>
</tr>
<tr align="center">
<td width="50%" align="right" width="100"><font color="navy">Password</font></td>
<td width="50%" align="left"><input type="password" size="10" name="pass"></td>
</tr>
<tr>
<td> </td>
</tr>
<tr align="center">
<td colspan="2" align="center"><input name="submit" type="submit" value="Inloggen"></td>
</tr>
</table>
</form>
<?php } ?>`
This is the code I've used. And I'm getting the following error:
Please Enable Cookies to Continue
To continue shopping at Amazon.com, please enable cookies in your Web browser. Learn more about cookies and how to enable them.
Once you have enabled cookies in your browser, please click on the button below to return to the previous page.
How can I fix this error? I've tried everything.
I'm using XAMPP at the moment.
Just for marking as answer:
you have to 2 things:
1 - Request login page from server using get method
2 - Send login data using new session and with hidden inputs
to see the script : https://stackoverflow.com/a/7532730/889678

Code can't be executed if is in a class function

I coded this snippet, and it works well when I use it alone without putting it in a class function, but when I put in a function in my class that had multi functions, the update and delete statements didn't work, although the insert function works well. I don't know why the update and delete don't work.
I checked the code for 4 hours, but I don't know what's the problem.
Is this a problem related to using the way to update multiple columns in a table?
public function admin_ads_manage(){
/*###############site_links#################*/
$sel_links="SELECT * FROM site_links ORDER BY id DESC";
$query_sel_links=mysql_query($sel_links);
// Count table rows
$count_links=mysql_num_rows($query_sel_links);
//add data
$site_name=$_POST['addname'];
$site_link=$_POST['addlink'];
if($_POST['addsubmit']){
$add_site_links="INSERT INTO site_links(site_name,site_link) VALUES ('$site_name','$site_link')";
$query_add_site_links=mysql_query($add_site_links);
echo "<meta http-equiv=\"refresh\"";
}
/*###############site_links#################*/
?>
<div id="adsform">
<form action="<?php echo $PHP_SELF; ?>" method="post">
<h1>ادارة أضف لنك موقعك</h1>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr><td>اسم الموقع:<input type="text" name="addname" ></td><td>الرابط:<input type="text" name="addlink"><td><input type="submit" name="addsubmit" value="اضف الرابط" ></td></td></tr>
<tr>
<td>
<table width="600" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>اسم الموقع</strong></td>
<td align="center"><strong>رابط الموقع</strong></td>
</tr>
<?php
while($fetch_site_links=mysql_fetch_array($query_sel_links)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $fetch_site_links['id']; ?>"></td>
<td align="center"><? $id[]=$fetch_site_links['id']; ?><? echo $fetch_site_links['id']; ?></td>
<td align="center"><input name="sitename[]" type="text" id="sitename" value="<? echo $fetch_site_links['site_name']; ?>"></td>
<td align="center"><input name="sitelink[]" type="text" id="sitelink" value="<? echo $fetch_site_links['site_link']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="تعديل"></td>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="حذف"></td>
</tr>
</table>
</td>
</tr>
</table>
<h1>اعلان اليوم</h1>
<h5 dir="rtl">لاضافة بنر لموضع اعلان اليوم ,كل ما عليك هو استبدال الصورة الموجودة فى مجلدimages بحيث تكون بنفس المقاسات والتى تساوى100 x 300</h5>
<p>adsense 728x90:الاعلان العرضى</p>
<p><textarea cols="50" rows="15" name="ads728"><?php echo $file728; ?></textarea></p>
<p>adsense 600x160:الاعلان المربع</p>
<p><textarea cols="50" rows="15" name="ads600"><?php echo $file600; ?></textarea></p>
<p>adsense 300x250:الاعلان الطولى</p>
<p><textarea cols="50" rows="15" name="ads300"><?php echo $file300 ?></textarea></p>
<p><input type="submit" name="submit" value="حفظ" /></p>
</form>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count_links;$i++){
$upd_site_links="UPDATE site_links SET site_name='$sitename[$i]', site_link='$sitelink[$i]' WHERE id='$id[$i]'";
$query_upd_site_links=mysql_query($upd_site_links);
echo "<meta http-equiv=\"refresh\">";
}
}
if($delete){
for($i=0;$i<$count_links;$i++){
$del_id = $checkbox[$i];
$del_links = "DELETE FROM site_links WHERE id='$del_id'";
$query_del_links= mysql_query($del_links);
}}
if($query_del_links){
echo "<meta http-equiv=\"refresh\" >";
}
if($query_upd_site_links){
echo "<meta http-equiv=\"refresh\" >";
}
}
Make sure that the code in the class function actually has access to all the variables you're using. I imagine that one of them isn't in scope.
$checkbox, $sitename, $sitelink, $id and $count_links may be the most likely culprits.
EDIT: Is all that one single function? You should break that up into smaller functions, each of which does one single thing.
There are numerous problems.
Where is the mysqli_connect() (or equivalent)? Your function seems to be relying on whatever connection handle the library last used. This is not good coding.
Your UPDATE loop sends a HTTP refresh for each row.
In fact, your action logic is littered through your display logic. This is also bad coding.
But the biggest thing is that you seem to be relying on register_globals and you aren't even aware of it. You should turn this off.
Your action code needs to retrieve all the variables it is looking for out of $_POST or $_GET (or $_REQUEST if it doesn't matter which). And each action (of which you have a couple) should only do what its asked to do once all the variables it needs are accounted for. It is really bad coding to assume that all field variables are automatically global variables.

How to send SMS using PHP CURL function

I have an account in a website by which I can send sms to mobile phones. In order to do that at first I need to log in using my id and password and then a page shows up where I put the recipient's mobile number and then my message and finally hit the button to send the message.
Now one of my friends told me that I can send sms from my own application through this website using PHP Curl function. I didn't have any prior idea about CURL function so I googled it but I couldn't figure out how to do this. I have checked the HTML code of the login page and the page from where I can send the sms of that website and I am posting it below.
Would you please kindly show me how to send sms using CURL function or any other way.. through this website..
Thanks in Advance :)
Form1
<form name="form" action="/websms/index.php" method="POST">
<input type="hidden" name="HTMLForm_formname" value="form">
<table align="center" size="300" border="0" class="list">
<tr class="r1">
<th colspan="3" class="left">
<label id="label_login_title" for="login_title" class="HTMLForm-label">User Login</label>
</th>
</tr>
<tr>
<td align="right" valign="top">
<label id="label_mobile_no" for="mobile_no" class="HTMLForm-label">Mobile Number</label>
</td>
<td>
<input type="text" id="mobile_no" name="mobile_no" size="20" maxlength="11" value="" onkeypress="return checkNumberOnly(event)" class="HTMLForm-text">
</td>
</tr>
<tr>
<td align="right" valign="top">
<label id="label_password" for="password" class="HTMLForm-label">Password</label>
</td>
<td>
<input type="password" id="password" name="password" value="" class="HTMLForm-password" size="20">
</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" id="submit" name="submit" value="Login" class="button_all_action">
<input type="hidden" id="submit_login" name="submit_login" value="1">
</td>
</tr>
</table>
</form>
Second Form
<form name="form" action="javascript:get(document.getElementById('form'));" method="POST">
<input type="hidden" name="HTMLForm_formname" value="form">
<table align="center" size="450" border="0" class="list">
<tr class="r2">
<th class="left">
<label id="label_send_to_no" for="send_to_no" class="HTMLForm-label">To</label>
</th>
<td class="left">
<textarea id="send_to_no" name="send_to_no" class="HTMLForm-textarea" onkeypress="checkValidGPNumner(document.form.send_to_no)" onchange="checkValidGPNumner(document.form.send_to_no)" onkeyup="checkValidGPNumner(document.form.send_to_no)" wrap="soft" style="width:250px;height:50px"></textarea>
</td>
</tr>
<tr class="r1">
<th class="left">
<label id="label_message" for="message" class="HTMLForm-label">Message</label>
</th>
<td class="left">
<textarea id="message" name="message" class="HTMLForm-textarea" onkeypress="textCounter(document.form.message,document.form.counter_message,160)" onchange="textCounter(document.form.message,document.form.counter_message,160)" onkeyup="textCounter(document.form.message,document.form.counter_message,160)" wrap="soft" style="width:250px;height:130px"></textarea>
<input type="text" id="counter_message" name="counter_message" size="5" value="" readonly="" class="HTMLForm-text"> <label id="label_char_remain" for="char_remain" class="HTMLForm-label">Character remained</label>
</td>
</tr>
<tr class="r2">
<td colspan="2" class="center">
<input type="submit" id="submit" name="submit" value="Send" class="button_all_action">
<input type="hidden" id="mid" name="mid" value="1">
<input type="hidden" id="submit_sms" name="submit_sms" value="1">
</td>
</tr>
</table>
</form>
Simple CURL Function to send sms:
function CURLsendsms($number, $message_body){
$api_params = $api_element.'?apikey='.$apikey.'&sender='.$sender.'&to='.$mobileno.'&message='.$textmessage;
$smsGatewayUrl = "http://springedge.com";
$smsgatewaydata = $smsGatewayUrl.$api_params;
$url = $smsgatewaydata;
$ch = curl_init(); // initialize CURL
curl_setopt($ch, CURLOPT_POST, false); // Set CURL Post Data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch); // Close CURL
// Use file get contents when CURL is not installed on server.
if(!$output){
$output = file_get_contents($smsgatewaydata);
}
}
You can indeed send SMS messages with cURL, but cURL is just one part of it. Using cURL, you'd make API calls to a provider like Twilio.
This is a solution using Twilio.
First of all you have to download the twilio library for PHP:
https://github.com/twilio/twilio-php/downloads
Then copy the Services folder in your Server and take in mind the location.
Now you have to create a simple program like the one below, (this is an example an easy php sms sender):
<?php //lets say that the name of this php is smsSender.php
$contactname = $_POST['name'];
$contactphone = $_POST['mobile_no'];
$message = $_POST['message'];
require 'Services/Twilio.php';//<<<<<<<<<HERE! make sure the path is ok.
$AccountSid = "AXXXXXX"; // this numbers you can find it in your twilio dashboard
$AuthToken = "TXXXXXXXXX";// also this number .
$client = new Services_Twilio($AccountSid, $AuthToken);
$people = array(
//"4566789903" => "Curious George",
$contactphone => $contactname,
);
foreach ($people as $number => $name) {
$sms = $client->account->sms_messages->create("7035960031",$number,
$message);
echo "Sent message to $name";
}
?>
So you will need to change in your forms the action like this:
<form name="form" action="smsSender.php" method="POST">
Note: if you are using a trial account, you will be only able to send to verified numbers in your account, but if you register a phone number (1USD/month) then you can send to any number and without the sandbox message).
IMPORTANT: Curl library must be installed in the php server, if you are using your own server, lets say in UBUNTU, this command will install those libraries:
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
After changing to this, the code worked great for me.
foreach ($people as $number => $name) {
$client->account->messages->sendMessage("+12055xxxxxx",$number,
$message);
I am not sure that you can send SMS's with CURL, however you can with PHP's mail function.
I have never done so however.
Curl basically used for scrapping contents from another web site, it doesnt have any function to send sms. You have to use other way.
there re few tools by which you can connect to your sms (text Server) from there you can send sms. you can use curl to get your sms replies or etc later part if you need and eager to use that.

Categories