I want to loginto a site using curl and then get resulting page. for example i want to run a script and see my yahoo mail page. i wrote something like this.
//set POST variables
$url = 'http://localhost/test/login';
$fields = array(
'username'=>'abc',
'password'=>'def'
);
$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_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
login.php
echo $_POST['username'];
echo $_POST['password'];
header('Location: http://www.xyz.com/');
script posts data to login.php but contents of xyz.com are not shown.
By logging into a Website you normaly generate a Session ID which is stored in a Session. By logging in with CURL you didn't save this cookie in your Client Browser, you maybe logged in with your server, but there is no way to be logged in with your client.
And of course change this line:
echo $_POST['username'];
echo $_POST['password'];
header('Location: http://www.xyz.com/');
to the following:
header('Location: http://www.xyz.com/');
echo $_POST['username'];
echo $_POST['password'];
or use ob_start(); on the beginning of your script.
...which doesn't make sence because you'll never see that content ;)
Your problem is the two echo calls before the call to header.
According to the documentation header must be called before any other output is sent.
http://php.net/manual/en/function.header.php
Related
Basically I want to have one centralized file (preferably .php or .txt).. In it I will define the version and online statuses of my 3 API's (login, register, and stats)
I will somehow link it to my system status page and call upon them in my html with like $version, $login, $register, or $stats and they will automatically display whatever is defined in the centralized file.
My stats page (https://epicmc.us/status.php).. I want to define it all from a seperate file and call upon it in the HTML.
I tried making an external file called check.php and put this in it:
<?php
$version = "1.0.0";
$login = 'online';
$register = 'online';
$stats = 'online';
echo json_encode(compact('version','login','register','stats'));
?>
and then in my stats page I called upon it with
<?php
$data= json_decode(file_get_contents('https://epicmc.us/api/bridge/check.php'),true);
echo $version;
echo $login;
echo $register;
echo $stats;
?>
The page is just blank though.
How would you go about implementing this into my stats page code?
http://pastebin.com/nREdfH1u
A good solution here would be to curl your file.
As you already return a JSON string containing your values, just curl your 'check.php' file and json_decode the response.
One of the advantages of this method is that you can access these informations from other domains.
You should be able to get all the values easily.
Example :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'check.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to return the response in a variable and not output it
// $result contains the output string
$result = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$array_response = json_decode($result, true);
// echo $array_response['version']...
I've been trying to use the code below to send sms but it does not send when I loop. It only works if I just pick one number from database. I have over 5,000 numbers in the database and wish to send an sms to all of them at the same time, Please help.
mysql_select_db($database_xxx, $xxx);
$query_rs = "SELECT phone FROM `notify` order by id asc LIMIT $l1 , $l2";
$rs= mysql_query($query_rs, $xxx) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs= mysql_num_rows($rs);
$phone = $row_rs['phone'];
// Do while loop to send sms.
while($row=mysql_fetch_assoc($rs)){
// Let's do some formatting and keep smiling.
$giringirin = ereg_replace("[^0-9]", "", $phone );
if (strlen($giringirin) == 11) {
$phone1=substr($giringirin, 1);
$phone= "234$phone1";
} elseif (strlen($giringirin) == 13){
$phone = $giringirin;
}
extract($_POST);
//set POST variables
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=$username&password=$password&message=$smsmessage&mobile=$phone&sender=$sender";
$fields = array(
);
//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_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
if ($result == '1801') { echo "SMS has also been sent to the Customer ($phone) \n";} else { echo "Oooops, No sms was sent";}
//close connection
curl_close($ch);
}
Your code is confusing...
curl_setopt($ch,CURLOPT_POST,count($fields));
CURLOPT_POST is a boolean flag. Either you're doing a post, or you're not. The number of fields you're posting is irrelevant.
You're building up a series of variables/values to be posted, but doing it via string operations. CURL is perfectly capable of taking an array and doing all that for you, reducing your entire foreach loop to just
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
You're using extract() on $_POST, which pollutes your script's variable namespace with any garbage a malicious user cares to send over - you're essentially replicating PHP's utterly moronically brain-dead register_globals all over again.
You're using ereg, which has been deprecated for approximiately 5 million internet years. You should be using the preg functions insteadl
What happens when you run this script in the browser? Blank page? Something? Error?
Start by changing
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=$username&password=$password&message=$smsmessage&mobile=$phone&sender=$sender";
to
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=".$username."&password=".$password."&message=".$smsmessage."&mobile=".$phone."&sender=".$sender."";
I`m trying to create a script sending information from 1 domain to another and saving the data to a database, however i retrieve data from domain 1, but cant save to the database.
Here is the script from domain 1:
// Get Domain Name:
$domain = $_SERVER['HTTP_HOST'];
// Get User IP Address:
$user = $_SERVER["REMOTE_ADDR"];
// Connect to Source:
$url = 'http://www.domain2.com/source.php';
$fields = array(
'id'=>'1',
'user'=>$user,
'domain'=>$domain,
);
//url-ify the data for the POST
$fields_string = '?';
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_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
Here is the script to save the data to the database on domain 2:
// Get domain and user details:
$domain=$_GET['domain'];
$user=$_GET['user'];
// Connection to MySQL Database.
include ('_includes/_dbconnection.php');
include ('_includes/_dbopen.php');
// Insert Data to MySQL Database
$sql="INSERT INTO traffic (url, cip)VALUES('$_POST[domain]', '$_POST[user]')";
$result=mysql_query($sql);
if($result){
echo $domain;
echo $user;
}
else {
echo 'Not Input';
}
You are posting the form with curl and trying to get the values with $_GET
Try to use $_POST or $_REQUEST
$domain=$_POST['domain'];
$user=$_POST['user'];
// Connection to MySQL Database.
include ('_includes/_dbconnection.php');
include ('_includes/_dbopen.php');
// Insert Data to MySQL Database
$sql="INSERT INTO traffic (url, cip)VALUES('$domain', '$user')";
$result=mysql_query($sql);
if($result){
echo $domain;
echo $user;
}else {
echo 'Not Input';
}
I searched all over, but have not seen anything related to this question. I have a curl script that obtains a random generated string of 10 characters from a website. I have received permission from the website owner to fetch this data and display it on my blog. My question is this when a user comes to my website it displays the string and if they refresh the page it will display a new string. How can I limit one string per user so that they cannot refresh over and over to obtain multiple strings?
Here is an example curl upon which my code is based:
$url = "http://m.www.yahoo.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
preg_match('/<dl class="markets clearfix strong small">(.*)<\/dl>/is', $output, $matches);
echo $matches[0];
curl_close($ch);
maybe you can use session variable to check if the user has already got a string.
function startSession(){
$id = session_id();
if ($id != '')
session_write_close();
if (isset($_COOKIE['PHPSESSID']) && $id != $_COOKIE['PHPSESSID']) {
session_write_close();
session_id($_COOKIE['PHPSESSID']);
}
session_start();
}
//Start your sessions
startSession();
if (!$_SESSION['UserGotAString']) {
echo "Some Error msg"
return;
}
session_write_close();
//Request to Yahoo and regex match comes here..
// Start a session
startSession();
$_SESSION['UserGotAString'] = true
echo $matches[0];
session_write_close();
Im not sure if this a good method. Also if you want to reset the session variable base on timer check this link
Set timeout in php
I want to decode Json returned from the WebService, And it should set a cookie, that i want to use to call next WebService API. I am not sure how to set a cookie, and decode this json.
I tried decoding it, but get the error. I need to extract sessionId. You can use the WebService.. I have put this on Internet.
Here is my code sample
<?php //extract data from the post
extract($_POST); //set POST variables
$url = 'http://202.83.243.119/ems/loginByEID.json';
$fields = array(
'eid'=>urlencode("7ea888b6-36e9-49db-84f3-856043841bef")
);
//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_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post $result =
curl_exec($ch);
//close connection curl_close($ch);
//decoding Json $obj =
json_decode($result);
print $obj->{'stat'};
?>
Use setcookie() to set a cookie.
Avoid extract() like the plague; it can be used to introduce any client-specified variables into your code.
Use $fields_string = http_build_query($_GET) to build a query string instead of your hodge-podge above.
Format your code properly. For example, you put the $obj = inside the previous comment line.