php program works locally but not on server - php

I have this code running locally smoothly. The program only looks for a file and write data on it. When I try to run the same program on a server, it just does nothing...
This is the complete code:
<?php
$myFile = "Current_User.txt";
//$produto = "sapato1";
//$produto = $produto.";";
$i = 0;
$produto = $_POST["produtoID"];
$produto = $produto.";";
//$produto = $_POST["produtoID"];
$fh = fopen($myFile, 'r');
$line_of_text = fgets($fh);
$str = $line_of_text;
$str = str_replace("\n", "", $str);
$str = $str."_Cesto.txt";
fclose($fh);
$fh2 = fopen($str, "r")or die("can't open file");
while (($line_of_text = fgets($fh2))) {
$i++;
$line_of_text = str_replace("\n", "", $line_of_text);
$line_of_text = str_replace("\r", "", $line_of_text);
if($produto == $line_of_text){
break;
}
}
fclose($fh2);
$dados = file($str);
if($i == 1){
unset($dados[$i - 1]);
}
else{
unset($dados[$i - 2]);
}
file_put_contents($str,$dados);
?>

Following code is facing the same problem so permissions could be the reason or elsewise change the method to GET it will be working.
<?php
$url = "http://sea-summit.com/T_webservice/get_appointments_by_id.php";
$data = array('user_id'=> 1);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" . "Accept: application/json\r\n" ));
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
var_dump($response);
?>

you forgot to upload Current_User.txt

Did you check the write permission of your Current_User.txt file?
What File System are you using on your Server?
If you have access: Check your PHP error_log on the Server to get more valueable answers, or change the error_reportings.

Related

use a random proxy in a php function without curl

function ID($name)
{
$name = trim($name);
$url = "$name";
$postdata = http_build_query(
array(
'url' => $url,
'check' => 'Lookup'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://www.website.net/search?='.$url.'');
$regex = '/og:image"\s*content="([^"]+)"/';
preg_match($regex, $result, $matches)
return($matches[1]);
This function is working fine, now i have 3 paid proxies with the same user:pass, and i want to randomize proxy every time the function is called so my website never use it's own Ip adress. I saw a lot of stuff about CURL I don't want to use that
I found this code but i don't know how to add it to the actual code:
$filename = getcwd() . "/proxies.json";
$file = fopen($filename, "r");
if ($file == false) { echo ("Error in opening file");
exit();
}
$filesize = filesize($filename);
$filetext = fread($file, $filesize);
fclose($file);
$proxies = json_decode($filetext, true);
$rand_proxy = array_rand($proxies);
$proxy = $proxies[$rand_proxy]["url"];
$proxyauth = $proxies[$rand_proxy]["auth"];
and this is the proxies.json file:
[{"url":"XX.XX.XXX.YY:80","auth":"user:pass"},
{"url":"XX.YY.XX.YYY:80","auth":"user:pass"},
{"url":"XX.XX.YYY.X:80","auth":"user:pass"}]

Cron job from cpanel retunning error while running script from URL is not reurning any error

I wrote a script on my web server. when I run my code using the URL (http://www.eurekabd.com/RMS/corn.php) and found ok. Then I made a corn job scheduler in cPanel by this command /usr/local/bin/php /home/eurekabd/public_html/RMS/corn.php. When cron job run this command it sends me mail that
Warning: file_get_contents(shakil.txt): failed to open stream: No such file or directory in /home/eurekabd/public_html/RMS/corn.php on line 12
<?php
$response = get_web_page("http://144.48.2.11/Test_PHP/live_check.php");
//$response = get_web_page("http://localhost/phpScriptTesting/request.php");
$txtFile = "shakil.txt";
echo $response;
/*if(! filesize( $txtFile ) )
{
echo " Not EMPTY FILE ";
}*/
echo "Server is =".$response;
if ((trim(file_get_contents('shakil.txt')) != false) && ($response == "OK")) {
echo "-->I have some data to send.";
//http://144.48.2.11/Test_PHP/index_2.php/?data={1239{fnc:15,sts:[0,8,25,526,19][0103044396424b7f0c][01030caf854366a45b436695684366376d][01030c9ba63b447bb33b7249523a1d4641][01030c00003f8000003f8000003f80d6f8][010304999a3f1924ba][x][x][x][x][x][0b03080113045403ff03f057ea][x]}})
$file_content = file_get_contents("shakil.txt");
$file_content_separated_by_spaces = explode(";", $file_content);
//print_r ($file_content_separated_by_spaces)."</br>";
$loop_size = sizeof($file_content_separated_by_spaces)-2;
echo $loop_size;
for ($x = 0; $x <= $loop_size; $x++) {
$dataFormatforServer = str_replace(' ', '_', $file_content_separated_by_spaces[$x]);
$textData = "http://144.48.2.11/Test_PHP/index_3.php/?data=".$dataFormatforServer;
//$textData = "http://localhost/phpScriptTesting/test.php/?data=".$dataFormatforServer;
echo $textData."</br>";
$sendData = get_web_page($textData);
//echo $sendData."</br>";
}
$file_to_delete = 'shakil.txt';
unlink($file_to_delete);
$my_file = 'shakil.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
}
else{
echo "I don't Have Any Data to send.";
}
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "test", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 300, // time-out on connect
CURLOPT_TIMEOUT => 300, // time-out on response
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
?>
you may add/change php.ini attributes for that..
allow_url_fopen = On
It may resolve the issue..

PHP Posting data to another php file hosted in a remote server using file_get_contents

I have access to 3 different remote servers. They all host the same php file, it's named processing.php.
And in a different server I have 3 pages :
index.html : that contains a form with POST method that send data to forward.php
forward.php : that forward the form values to processing.php on the other servers
processing.php : Display the posted data
The Problem : the code in processing.php is not executed and the returned result is a plain text of the source code of processing.php!!
forward.php :
$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
rtrim($_POST['listserver'],"-");
$listServer = explode("-",$_POST['listserver']);
foreach ($listServer as $server){
if(!empty($server)){
$url = 'http://'.$server.'/processing.php';
$data = array('field1' => $field1, 'field2' => $field2);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
echo "You can't ";
}else{
echo ('result : '.$result);
}
}
}
Processing.php
$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$result = $field1+$field2;
$myFile = "files/result.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $result);
fclose($fh);
But in all the servers, result.txt is empty!!
Thanx to #MarkusZeller suggestion, I managed to get it work using cURL.
This thread was very helpful, thank you Stack Over Flow community !

PHP file_get_contents and cURL raise http error 500 internal server

I am using a function inside a PHP class for reading images from array of URLs and writing them on local computer.
Something like below:
function ImageUpload($urls)
{
$image_urls = explode(',', $urls);
foreach ($image_urls as $url)
{
$url = trim($url);
$img_name = //something
$source = file_get_contents($url);
$handle = fopen($img_name, "w");
fwrite($handle, $source);
fclose($handle);
}
}
It successfully read and write 1 or 2 images but raise 500 Internal severs for reading 2nd or 3rd image.
There is nothing important in Apache log file. Also i replace file_get_contents command with following cURL statements, but result is the same (it seems cURL reads one more image than file_get_contents).
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,500);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
$source = curl_exec($ch);
curl_close($ch);
unset($ch);
Also the problem is only for reading from http URLs, and if I have images on somewhere local, there is no problem for reading and writing them.
I don't see any handler for reading in the loop , your $handle = fopen($img_name, "w"); is just for writing , you also need $handle = fopen($img_name, "r"); for reading ! because you can't read handle (fread () ) for fopen($img_name, "w");.
Additional answer :
Could you modify to (and see if it works):
.........
$img_name = //something
$context = stream_context_create($image_urls );
$source= file_get_contents( $url ,false,$context);
.....
.....
I have made some changed to your code, hope that helps :)
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"Content-Type: text/html; charset=utf-8"
)
);
$context = stream_context_create($opts);
$image_urls = explode(',', $urls);
foreach ($image_urls as $url) {
$result = file_get_contents(trim($url),TRUE,$context);
if($result === FALSE) {
print "Error with this URL : " . $url . "<br />";
continue;
}
$handle = fopen($img_name, "a+");
fwrite($handle, $result);
fclose($handle);
}

PHP - Read a text file and use each line value sequentially in POST requests

Hello :) I apologize in advance if this is really dumb, but I've been trying to figure it out for a week and now I'm about to explode :D
I have a list of 200 ID numbers in a text file, each are separated by a newline. I want to use PHP to use each line, one by one, as a variable in a POST request. I can use this code to send the first line, by specifying the number of characters to include from the file:
$IDfile = "file.txt";
$fh = fopen($IDfile, 'r');
$ID = fread($fh, 18);
fclose($fh);
Then I can use the $ID variable in the POST. But this is only good for the first line, since there are 18 characters in the line, but there are 199 more lines.
I can also print all the lines in the file with a foreach, but this won't help with the POST requests:
$IDfile = "file.txt";
$lines = file($IDfile);
foreach($lines as $line_num => $line)
{
echo $line;
echo "<br>";
}
So, here is the full script of my POST request, to send a "destroy tweet" command to the Twitter API 1.1 - The $ID variable is the one I want to replace using every line in the text file sequentially:
require_once('TwitterAPIExchange.php');
include('apikeys.php');
$url = "https://api.twitter.com/1.1/statuses/destroy/" .$ID . ".json";
$requestMethod = 'POST';
$postfields = array(
);
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
So how do I do it? How can I get each line sequentially from the text file, and then use the single line value as a variable in the POST request, until each has been read?
This is probably way out of my league lol, but I am determined to learn this, and any help or direction would be so very gratefully received :)
Try this, if i am right your must contain the id field
<?php
require_once( "TwitterAPIExchange.php" );
include( "apikeys.php" );
$url = "https://api.twitter.com/1.1/statuses/destroy/%s.json";
$lines = file( "file.txt" );
$post = array(
"id" => null
);
foreach( $lines as $line ) {
$line = trim( $line );
$post["id"] = $line;
$twitter = new TwitterAPIExchange( $settings );
$response = $twitter->buildOauth( sprintf( $url, $line ), "POST" )
->setPostfields( $post )
->performRequest();
$response = json_decode( $response );
if ( !$response ) {
echo "Empty response";
break;
}
var_dump( $response );
}
?>
Use fopen to read the file and then inside the loop you can create the URL and send a POST request, like so:
$f = fopen("file.txt", "r");
while(!feof($f)) {
$url = "https://api.twitter.com/1.1/statuses/destroy/" . trim($f) . ".json";
$requestMethod = 'POST';
$postfields = array(
);
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
}
Something along these lines should help you solve that issue
$IDfile = "file.txt";
$lines = file($IDfile);
foreach($lines as $line_num => $line)
{
echo sending request for $line;
$url = "https://api.twitter.com/1.1/statuses/destroy/" .$line . ".json";
$requestMethod = 'POST';
$postfields = array(
);
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
}

Categories