I am trying to check if an e-mail address a user enters already is in the list of subscribers or not.
This is what I have tried so far:
<?php
$apikey = 'apikey';
$list_id = 'listid';
$chunk_size = 4096; //in bytes
$url = 'http://us14.api.mailchimp.com/export/1.0/list?apikey='.$apikey.'&id='.$list_id;
/** a more robust client can be built using fsockopen **/
$handle = #fopen($url,'r');
if (!$handle) {
echo "failed to access url\n";
} else {
$i = 0;
$header = array();
while (!feof($handle)) {
$buffer = fgets($handle, $chunk_size);
if (trim($buffer)!=''){
$obj = json_decode($buffer);
if ($i==0){
//store the header row
$header = $obj;
} else {
//echo, write to a file, queue a job, etc.
echo $obj[0];
}
$i++;
}
}
fclose($handle);
}
?>
This will return all email subscribers. I am trying to narrow it down to the specific e-mail the user entered, but I am stuck and don't know how to do it?
/** a more robust client can be built using fsockopen **/
$handle = #fopen($url,'r');
if (!$handle) {
echo "failed to access url\n";
} else {
$i = 0;
$header = array();
while (!feof($handle)) {
$buffer = fgets($handle, $chunk_size);
if (trim($buffer)!=''){
$obj = json_decode($buffer);
if ($i==0){
//store the header row
$header = $obj;
} else {
//echo, write to a file, queue a job, etc.
if ($obj[0] == "myemail#gmail.com") { echo "XXXXXXX";} else { }
}
$i++;
}
}
fclose($handle);
}
?>
Related
With this script, change the order of the lines in a text file, but I'd like to save the file as a new file. How can I do that?
<?php
$file = "menu2.txt";
$righe = file($file);
$numrighe = count($righe);
$portieri = array();
$difensori = array();
$aladestra = array();
$alasinistra = array();
$attaccante = array();
for($i=0; $i < $numrighe;$i++) {
$riga = $righe[$i];
list($giocatore, $ruolo) = preg_split("[>]", $riga);
if(strcmp($ruolo,"Portiere")) { array_push($portieri, $giocatore); }
else if(strcmp($ruolo,"Difensore")) { array_push($difensori, $giocatore); }
else if(strcmp($ruolo,"Ala destra")) { array_push($aladestra, $giocatore); }
else if(strcmp($ruolo,"Ala sinistra")) { array_push($alasinistra, $giocatore); }
else if(strcmp($ruolo,"Attaccante")) { array_push($attaccante, $giocatore); }
}
?>
Excuse me but probably because of google translator or because they are prevented, but you probably will not understand it, I did it:
<?php
$file = "menu2.txt";
$righe = file($file);
$numrighe = count($righe);
$portieri = array();
$difensori = array();
$aladestra = array();
$alasinistra = array();
$attaccante = array();
for($i=0; $i < $numrighe;$i++) {
$riga = $righe[$i];
list($giocatore, $ruolo) = preg_split("[>]", $riga);
if(strcmp($ruolo,"Portiere")) { array_push($portieri, $giocatore); }
else if(strcmp($ruolo,"Difensore")) { array_push($difensori, $giocatore); }
else if(strcmp($ruolo,"Ala destra")) { array_push($aladestra, $giocatore); }
else if(strcmp($ruolo,"Ala sinistra")) { array_push($alasinistra, $giocatore); }
else if(strcmp($ruolo,"Attaccante")) { array_push($attaccante, $giocatore); }
}
// open your new file
$newFile = fopen('newmenu2.txt', 'w');
// write to your file using $newFile file handler,
// with the imploded data you want to write into your file
fwrite($newFile, implode("\n", $data));
// close your file handler
fclose($newFile);
?>
Notice: Undefined variable: data on line 29
Warning: implode(): Invalid arguments passed on line 29
I've got this code that logs to a .txt file in a user folder. I need it to refresh in real time or 1 or 5 seconds.
The code basically logs a visitor that viewing the page, so it will say: Viewers: 1 and when the visitor exit the page it will go back to Viewers: 0
The script is working but I want it to refresh.
Any ideas?
Here the PHP Code:
require_once 'dbconfig.php';
if ($_GET[user] != ""){
$check_query = mysql_query("SELECT * FROM profiles WHERE username='$lowerusername'");
while ($display = mysql_fetch_array($check_query)) {
$username = $display['username'];
}}
$dataFile = "profile/$username/visitors.txt";
$sessionTime = 1; //this is the time in **minutes** to consider someone online before removing them from our file
//Please do not edit bellow this line
error_reporting(E_ERROR | E_PARSE);
if(!file_exists($dataFile)) {
$fp = fopen($dataFile, "w+");
fclose($fp);
}
$ip = $_SERVER['REMOTE_ADDR'];
$users = array();
$onusers = array();
//getting
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
$users[] = rtrim(fgets($fp, 32));
}
flock($fp, LOCK_UN);
fclose($fp);
//cleaning
$x = 0;
$alreadyIn = FALSE;
foreach($users as $key => $data) {
list( , $lastvisit) = explode("|", $data);
if(time() - $lastvisit >= $sessionTime * 10) {
$users[$x] = "";
} else {
if(strpos($data, $ip) !== FALSE) {
$alreadyIn = TRUE;
$users[$x] = "$ip|" . time(); //updating
}
}
$x++;
}
if($alreadyIn == FALSE) {
$users[] = "$ip|" . time();
}
//writing
$fp = fopen($dataFile, "w+");
flock($fp, LOCK_EX);
$i = 0;
foreach($users as $single) {
if($single != "") {
fwrite($fp, $single . "\r\n");
$i++;
}
}
flock($fp, LOCK_UN);
fclose($fp);
if($uo_keepquiet != TRUE) {
echo '<strong>Viewers:</strong> ' . $i . '';
}
I am creating a socket script that will listen to our 3 devices.and the device is setup in one server ip and one port only.
$file = fopen('txt.log','a+');
$server = stream_socket_server('tcp://'.$ipserver.':'.$port, $errno, $errorMessage);
if(!$server) {
echo "$errorMessage ($errno)<br />\n";
}
else{
while($client = #stream_socket_accept($server,$timeout)) {
stream_copy_to_stream($client, $file);
fclose($file);
fclose($client);
}
}
but the problem is that if one device is connected,the two devices cannot connect anymore.I appreciate some one can help me how to get this work.or give me some idea
Thank you in advance.
$file = fopen('txt.log', 'a+');
$server = stream_socket_server("tcp://$ipserver:$port", $errno, $errorMessage);
if (!$server)
echo "$errorMessage ($errno)<br />\n";
else
{
$s = array($server);
$t = $timeout == -1 ? NULL : $timeout;
while ($r = $s and stream_select($r, $n=NULL, $n=NULL, $t))
foreach ($r as $stream)
if ($stream == $server) // new client
$s[] = stream_socket_accept($server, -1);
else
if (!fputs($file, fgets($stream)))
{
fclose($stream);
array_splice($s, array_search($stream, $s), 1);
}
}
i need a help ^^
What i need is script which will open and read all .csv files in folder 'csv/files' and then do that thing in "if". Well, when i had only one file it worked fine. I managed to construct some script which is not working but no "error line" popping up either ...
So can somebody look at my code and tell me what i am doing wrong ?
<?php
foreach (glob("*.csv") as $filename) {
echo $filename."<br />";
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$url = $data[0];
$path = $data[1];
$ch = curl_init($url);
$fp = fopen($path, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
fclose($handle);
}
}
?>
This is a prime candidate for multi-threading, and here's some code to do it:
<?php
class WebWorker extends Worker {
public function run() {}
}
class WebTask extends Stackable {
public function __construct($input, $output) {
$this->input = $input;
$this->output = $output;
$this->copied = 0;
}
public function run() {
$data = file_get_contents($this->input);
if ($data) {
file_put_contents(
$this->output, $data);
$this->copied = strlen($data);
}
}
public $input;
public $output;
public $copied;
}
class WebPool {
public function __construct($max) {
$this->max = $max;
$this->workers = [];
}
public function submit(WebTask $task) {
$random = rand(0, $this->max);
if (isset($this->workers[$random])) {
return $this->workers[$random]
->stack($task);
} else {
$this->workers[$random] = new WebWorker();
$this->workers[$random]
->start();
return $this->workers[$random]
->stack($task);
}
}
public function shutdown() {
foreach ($this->workers as $worker)
$worker->shutdown();
}
protected $max;
protected $workers;
}
$pool = new WebPool(8);
$work = [];
$start = microtime(true);
foreach (glob("csv/*.csv") as $file) {
$file = fopen($file, "r");
if ($file) {
while (($line = fgetcsv($file, 0, ";"))) {
$wid = count($work);
$work[$wid] = new WebTask(
$line[0], $line[1]);
$pool->submit($work[$wid]);
}
}
}
$pool->shutdown();
$runtime = microtime(true) - $start;
$total = 0;
foreach ($work as $job) {
printf(
"[%s] %s -> %s %.3f kB\n",
$job->copied ? "OK" : "FAIL",
$job->input,
$job->output,
$job->copied/1024);
$total += $job->copied;
}
printf(
"[TOTAL] %.3f kB in %.3f seconds\n",
$total/1024, $runtime);
?>
This will create a maximum number of pooled threads, it will then read through a directory of semi-colon seperated csv files where each line is input;output, it will then submit the task to read the input and write the output asynchronously to the pool for execution, while the main thread continues to read csv files.
I have used the simplest input/output file_get_contents and file_put_contents so that you can see how it works without cURL.
The worker selected when a task is submitted to the pool is random, this may not be desirable, it's possible to detect if a worker is busy but this would complicate the example.
Further reading:
https://gist.github.com/krakjoe/6437782
http://php.net/pthreads
I have the following output from a PHP script:
one#gmail.com test1
two#gmail.com test2
which is generated from the following:
$i = 0;
$header = array();
while (!feof($handle)) {
$buffer = fgets($handle, $chunk_size);
if (trim($buffer)!=''){
$obj = json_decode($buffer);
echo $obj[0]." ".$obj[2]."<br>";
$i++;
}
}
fclose($handle);
How can i convert the output of the script into a JSON format of :
{"emails":[{"email":"one#gmail.com","option":test1"},{"email":"two#gmail.com","option":test2"}]}
The script was taken from the Mailchimp API which list the subscribers of a list.
Here is the script for reference:
<?php
$apikey = '1234-us7';
$list_id = '1234';
$chunk_size = 4096; //in bytes
$url = 'http://us7.api.mailchimp.com/export/1.0/list?apikey='.$apikey.'&id='.$list_id.'&output=json';
/** a more robust client can be built using fsockopen **/
$handle = #fopen($url,'r');
if (!$handle) {
echo "failed to access url\n";
} else {
$i = 0;
$header = array();
while (!feof($handle)) {
$buffer = fgets($handle, $chunk_size);
if (trim($buffer)!=''){
$obj = json_decode($buffer);
if ($i==0){
//store the header row
$header = $obj;
} else {
//echo, write to a file, queue a job, etc.
echo $obj[0]." ".$obj[2]."<br>";
}
$i++;
}
}
fclose($handle);
}
?>
Thank you!
It appears to already be in JSON, because you are using json_decode to get that output. So just... stop using json_decode on it.
As #jessica has already mentioned, it appears that $buffer is coming to you as JSON, because you are running json_decode(&buffer) on it.
However, if you want to do some manipulations, arrange the data so you build an array like this:
$myArray = array(
'emails' => array(
array('email' => 'one#gmail.com','option' => 'test1'),
array('email' => 'two#gmail.com','option' => 'test2'),
)
);
Then:
echo json_encode(myArray);
Using your supllied code, it would be something like this (untested):
$myArray = array();
$i = 0;
$header = array();
while (!feof($handle)) {
$buffer = fgets($handle, $chunk_size);
if (trim($buffer)!='') {
$obj = json_decode($buffer);
$myArray['emails'][] = array('email' => $obj[0],'option' => $obj[2]);
$i++;
}
}
fclose($handle);
echo json_encode($myArray);
$i = 0;
$result = array(); //create a new array
$header = array();
while (!feof($handle)) {
$buffer = fgets($handle, $chunk_size);
if (trim($buffer)!=''){
$obj = json_decode($buffer);
//echo $obj[0]." ".$obj[2]."<br>"; //comment out this line
$result[] = array('email' => $obj[0], 'option' => $obj[2]); //push new obj to the array
$i++;
}
}
fclose($handle);
echo json_encode(array('emails' => $result)); // convert to json format