PHP script execute show results during execution - php

How can I make that when I execute PHP script from my browser on webserver it shows results of that script while it's still executing (the code has many curls and it takes about 30 minutes to fully process). I have two servers and on one it shows every "echo" when it's called, but on the other server it shows all after 30 minutes, when script is fully executed. Both servers are running on apache, php 5.6
code:
<?php
error_reporting('E_ALL');
ini_set('error_reporting', 'E_ALL');
set_time_limit ( 2);
$i=0;
$handle = fopen("filmyNasze.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = explode("##", $line);
$nazwafilmu = trim($line[0]);
$linkfilmu = trim($line[1]);
$linkfilmu = 'http://xxx.pl' . $linkfilmu;
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $linkfilmu);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: cda.player=html5"));
curl_exec($ch);
$result = curl_exec($ch);
curl_close($ch);
libxml_use_internal_errors(true);
$doc2 = new DOMDocument();
$doc2->loadHTML($result);
$divid = str_replace('/video/', '', trim($line[1]));
foreach( $doc2->getElementsByTagName('div') as $div ) {
if($div->getAttribute('id') == 'mediaplayer' . $divid) {
$array = json_decode($div->getAttribute('player_data'), true);
//echo $array["video"]["file"] . " ## ";
}
}
echo $nazwafilmu . ' ## ' . trim($line[1]) . ' ## ' . $array["video"]["file"] . '<br />';
}
fclose($handle);
}
else {
die('brak pliku .txt');
}

You can flush PHP's output buffer using ob_flush() and flush().
http://php.net/manual/en/function.flush.php
<?php
ob_start();
echo 'Something';
ob_flush();
flush();
ob_end_flush();

Related

Curl request not flushing data periodically

I am making a curl request to a function and in that function the data is being flushed periodically. So I want to display the data as soon as it is flushed in my calling function. But my response is displayed collectively after the request is over. I want to display response side by side.
Code
require_once 'mystream.php';
stream_wrapper_register("var", "mystream") or die("Failed to register protocol");
$myVar = '';
// Open the "file"
$fp = fopen("var://myVar", "r+");
// Configuration of curl
$ch = curl_init();
$output = ' ';
$url = '';
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $output);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0 );
//curl_setopt($ch, CURLOPT_BUFFERSIZE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp); // Data will be sent to our stream ;-)
curl_exec($ch);
curl_close($ch);
// Don't forget to close the "file" / stream
fclose($fp);
mystream.php code
<?php
class mystream{
protected $buffer = '';
function stream_open($path, $mode, $options, &$opened_path) {
return true;
}
public function stream_write($data) {
ob_start();
echo $data;
ob_end_flush();
ob_flush();
flush();
// Extract the lines ; on y tests, data was 8192 bytes long ; never more
$lines = explode("\n", $data);
// The buffer contains the end of the last line from previous time
// => Is goes at the beginning of the first line we are getting this time
$lines[0] = $this->buffer . $lines[0];
// And the last line os only partial
// => save it for next time, and remove it from the list this time
$nb_lines = count($lines);
$this->buffer = $lines[$nb_lines-1];
unset($lines[$nb_lines-1]);
// Here, do your work with the lines you have in the buffer
//var_dump($lines);
//echo '<hr />';
return strlen($data);
}
}
Any leads would be highly appreciated.

Using cURL and foreach, huge loading time

I have the following code:
<?php
error_reporting(E_ALL & ~E_NOTICE);
set_time_limit(1000);
$f = $_GET['location'].'.txt';
if ( !file_exists($f) ) {
die('Location unavailable');
}
$file = fopen($f, "r");
$i = 0;
while (!feof($file)) {
$members[] = fgets($file);
}
fclose($file);
function get_thumbs($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
foreach ($members as $id){
// echo $id.'<br/>'; // do something with each line from text file here
$id = preg_replace('/\s+/', '', $id);
$link = 'http://localhost/cvr/get.php?id='.$id.'&thumb=yes&title=yes';
$content = get_thumbs($link);
echo $content;
}
?>
Where get.php is using almost the same above cURL function to grab data from a website and parse it.
In the txt file I have like 20 ids to get data from but the foreach seems to take a very long time to load, like 30+ seconds. Any advice?
I am a php beginner so please don't be very hard on me.
Thanks!

CURL work on localhost not on server

i am totaly new to CURL thing . i have a problem .
i have the below script which supposed to get remote files using a proxy , i am trying this code on my local host machine and it is working great "done successfully part: .
when i run it from server , it just keep going to the "invalid proxy" although it is same proxy as used in the local host ..
<?php
$dots = '';
include($dots.'classes/config.php');
$wordsLimitPerCall = 1;
$maxAttemptsTrials = 3;
$timeout = 10; // Seconds
$proxy = sqlSelectRow('proxy', array(
'condition'=>'not_valid=0',
'order'=>'rand()'
));
echo '<ul>';
if($proxy)
echo '<li>Using proxy: '.$proxy['proxy'].':'.$proxy['port'].'</li>';
$wordsRes = sqlSelectRows('words', array(
'condition'=>'is_done=0 and trials<'.$maxAttemptsTrials,
'limit'=>$wordsLimitPerCall,
'order'=>'created asc'
));
while($arr = $wordsRes->fetch_assoc()) {
$word = urlencode($arr['word']);
$url = 'http://example.com'.$word;
$callSettings = array(
'method'=>'curl',
'timeout'=>$timeout
);
if($proxy)
$callSettings['proxy'] = $proxy['proxy'].':'.$proxy['port'];
$result = callRemoteURL($url, $callSettings);
$filename = urldecode($word);
$filename = 'file/'.$arr['id'].' - '.$filename.'.mp3';
echo '<li>Getting the word: '.$word.'</li>';
if($result && strpos($result, '<html')===false) {
if(file_exists($dots.$filename))
unlink($dots.$filename);
fileWrite($filename, $result);
$is_done = 1;
echo '<li>done successfully</li>';
}
else {
$is_done = 0;
if($proxy) {
sqlUpdate('proxy', array(
'not_valid'=>0
), array(
'condition'=>'id='.$proxy['id']
));
echo '<li>Proxy is not valid</li>';
}
}
sqlUpdate('words', array(
'trials'=>$arr['trials']+1,
'path'=>$url,
'last_call_date'=>date('Y-m-d H:i:s'),
'is_done'=>$is_done
), array(
'condition'=>'id='.$arr['id']
));
}
echo '</ul>'; ?>
below the function called from utils
<? php
if($set['method']=='curl' && extension_loaded('curl'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, $set['timeout']); // Seconds
if($set['proxy'])
curl_setopt($ch, CURLOPT_PROXY, $set['proxy']);
ob_start();
if(curl_exec($ch) === false)
{
$errorNo = curl_errno($ch);
if($errorNo==28)
print_r('The connection took time more than '. $set['timeout'].' seconds, so we disconnected the process');
else
if($errorNo!=7)
print_r(curl_error($ch));
$result=false;
}
else
{
$result = ob_get_contents();
}
ob_end_clean();
return $result;
}
?>
sorry if its too long ... but i've spent the last couple days tryng to find out . any idea or what shall i do ?

File_get_contents($url): failed to open stream

I have a script where I read a file using:
file_get_contents(urlencode($url));
I get this error:
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request
I tried this, but I still get the error.
I've tried this:
ini_set('default_socket_timeout', 120);
This:
$opts = array('http'=>array('timeout' => 120));
$context = stream_context_create($opts);
$resul = file_get_contents($url,0,$context);
And this:
$opts = array('http'=>array('timeout' => 120,'header'=>'Connection : close'));
$context = stream_context_create($opts);
$resul = file_get_contents($url,false,$context);
Can you help me figure out why I get the error?
You need encode only "querystring", extract query and enconding this, after append enconded query you "url".
Note: file_get_contents requires allow_url_fopen=On in "php.ini", try use curl
Example (read my comments in code)
Note: This example get error in connection and http errors
<?php
//Set your page example
$uri = 'http://localhost/path/webservice.php?callback=&id=153&provenance=153&ret=a:1:{s:5:"infos";a:8:{s:8:"civilite";s:3:"Mme";s:5:"lname";s:0:"";s:5:"fname";s:8:"Nathalie";s:5:"email";s:17:"tometnata#free.fr";s:3:"tel";s:0:"";s:7:"adresse";s:0:"";s:6:"date_n";s:14:"10:"01/06/1969";s:2:"cp";s:0:"";}}';
//extract url
$parsed_url = parse_url($uri);
//Create fixed url
$fixed_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];
//If exists query
if (isset($parsed_url['query'])) {
$output = array();
$result = array();
//Extract querystring
parse_str($parsed_url['query'], $output);
//Encode values in querystring
forEach($output as $k => $v) {
$result[] = $k . '=' . rawurlencode($v);
}
//Append encoded querystring
$fixed_url .= '?' . implode('&', $result);
}
echo 'GET url: ', $fixed_url, '<br>';
//Get result in page
$ch = curl_init();
$timeout = 30; //set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $fixed_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
$errornum = curl_errno($ch);
$info = curl_getinfo($ch);
$status = (int) $info['http_code'];
if ($errornum !== 0) {
echo 'Error: ', curl_error($ch);
$file_contents = NULL;
} else if ($status !== 200) {
echo 'http_error: ', $status;
$file_contents = NULL;
} else {
echo 'Result:<hr>';
echo $file_contents;
}
curl_close($ch);
?>
Enable curl
Windows (Xampp): https://stackoverflow.com/a/1347340/1518921
Linux (like debian): https://stackoverflow.com/a/11724633/1518921
Mac OSX (probably outdated): https://stackoverflow.com/a/11354731/1518921

php curl functions issue

I am having troble on curl functions in my codes. My CURLINFO_HTTP_CODE always return 0 and when I use
curl_error($ch) it returns 'could not reach host'. My host is ispeech and it shouldn't have problems. Can anyone here help me out? Thanks a lot!
iSpeech.php
class iSpeechBase{
var $server;
var $parameters = array("device-type"=>"php-SDK-0.3");
function setParameter($parameter, $value){
if ($parameter == "server")
$this->server = $value;
else
$this->parameters["$parameter"] = $value;
}
function makeRequest(){
$ch = curl_init();
$url=$this->server . "/?" . http_build_query($this->parameters);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
ob_start();
echocurl_exec($ch);
$http_body = ob_get_contents();
ob_end_clean();
echo curl_getinfo($ch, CURLINFO_HTTP_CODE); //return 0
echo curl_error($ch); //return Could not reach host.
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
if ($this->parameters["action"] == "convert")
return array("error" => $http_body);
return $http_body;
}
}
synthesis-demo.php
require_once('ispeech.php');
$SpeechSynthesizer = new SpeechSynthesizer();
$SpeechSynthesizer->setParameter("server", "http://api.ispeech.org/api/rest");
$SpeechSynthesizer->setParameter("apikey", "myapikey");
$SpeechSynthesizer->setParameter("text", "yes");
$SpeechSynthesizer->setParameter("format", "wav");
$SpeechSynthesizer->setParameter("voice", "usenglishfemale");
$SpeechSynthesizer->setParameter("output", "rest");
$result = $SpeechSynthesizer->makeRequest();
So save faffing around with the ob buffer, you could replace
ob_start();
echocurl_exec($ch);
$http_body = ob_get_contents();
ob_end_clean();
with
$http_body = curl_exec($ch);
Also adds the space between echo and curl_exec missing from your example (although that should throw a fatal and stop execution - do you have your own error handler?)

Categories