The last unknown symbol in file when file_get_contents is used - php

I have a small problem. When I fetch a page via file_get_contents, the unknown symbol is concantenated to output which destroys the XML structure.
How can I solve the problem?
code:
file_get_contents('https://emea2cps.adobeconnect.com/api/xml?action=login&login=EMAIL&password=PASSWORD');
$cookies = array();
foreach ($http_response_header as $hdr) {
if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
parse_str($matches[1], $tmp);
$cookies += $tmp;
}
}
//print_r($cookies);
//echo "//////////////////";
$cook=$cookies['BREEZESESSION'];
echo $cook;
$opts = array(
'http'=>array(
'method'=>'GET',
'header'=>'Cookie: BREEZESESSION='.$cook.'\r\n',
)
);
$context = stream_context_create($opts);
echo "////////////////////////";
// Open the file using the HTTP headers set above
//$file = file_get_contents('http://www.example.com/', false, $context);
$file1 = file_get_contents('https://meet77842937.adobeconnect.com/api/xml?action=report-my-meetings', false, $context);
print_r($file1);
$xml = new SimpleXMLElement($file1);
output:
em2breezgwbhxopvpvknsux7////////////////////////sample1aksamaimeet77842937.adobeconnect.com/sample1/2014-02-28T06:15:00.000-08:002014-02-28T07:15:00.000-08:00true01:00:00.000sample2meet77842937.adobeconnect.com/sample2/2014-02-28T06:15:00.000-08:002014-02-28T07:15:00.000-08:00true01:00:00.000lastTtmeet77842937.adobeconnect.com/lastone/2014-02-28T15:30:00.000-08:002014-02-28T18:00:00.000-08:00false02:30:00.000�
Look at the last symbol. It must not exist ideally.
With regards

Related

get content of search result with file_get_content php

is there a possible way to get content of search result by file_get_content. I am trying to do this site's search results.
http://brillia.com/search/?attribute=1&area=13900,13100,13200,14999,12999,11999
but it's not giving me the content of this part ?attribute=1&area=13900,13100,13200,14999,12999,11999 is it something missing in my function. Or file_get_content is not enough for this?
function pageContent(String $url): \DOMDocument
{
$html = cache()->rememberForever($url, function () use ($url) {
$opts = [
"http" => [
"method" => "GET",
"header" => "Accept: text/html\r\n"
]
];
$context = stream_context_create($opts);
$file = file_get_contents($url, false, $context);
return $file;
});
$parser = new \DOMDocument();
libxml_use_internal_errors(true);
$parser->loadHTML($html = mb_convert_encoding($html,'HTML-ENTITIES', 'ASCII, JIS, UTF-8, EUC-JP, SJIS'));
return $parser;
}
The URL you're using is making another Ajax call, which is:
http://brillia.com/api/search/?area=13900,13100,13200,14999,12999,11999&key=2CsR0Bzv&mode=1&attribute=1&area=13900%2C13100%2C13200%2C14999%2C12999%2C11999&_=1552729056711
This will give you the desired result.
<?php
function pageContent( $url ) {
header('Content-type: text/html; charset=EUC-JP');
echo '<base href="http://brillia.com">';
echo file_get_contents($url);
}
echo pageContent('http://brillia.com/search/?attribute=1&area=13900,13100,13200,14999,12999,11999');

PHP : file_get_contents not working properly

I have a html page which sends a get request to php.
This is the code snippet in the php file
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>'GET',
)
);
$context = stream_context_create($opts);
//echo("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token={$_GET['token']}&agencyName={$_GET['agency']}&stopName={$_GET['stopname']}");
// Open the file using the HTTP headers set above
$file = file_get_contents("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token={$_GET['token']}&agencyName={$_GET['agency']}&stopName={$_GET['stopname']}", false, $context);
//$file = file_get_contents("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token=123-456-789&agencyName=SF-MUNI&stopName=The%20Embarcadero%20and%20Folsom%20St", false, $context);
echo(json_encode(simplexml_load_string($file)));
?>
Developer Console Output :
Warning: file_get_contents(http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token=123-456-789&amp;agencyName=BART&amp;stopName=Powell St. (SF)): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD_REQUEST
As you can see from the developer console output, in the url request sent there are BART&amp;stopName amp;amp; being inserted in the url which I'm not doing. The request fails due to this. Any solution around this?
Try the below code, this will make sure that you're stuff is properly URI encoded.
$params = [
'token' => $_GET['token'],
'agencyName' => $_GET['agency'],
'stopName' => $_GET['stopname']
];
$file = file_get_contents(sprintf("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?%s", http_build_query($params));
echo(json_encode(simplexml_load_string($file)));
Try this one:
$data = array('token'=>$_GET['token'],
'stopname'=>$_GET['stopname'],
'agency'=>$_GET['agency'],
);
$url = "http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?".$data;
$file = file_get_contents($url, false, $context);
echo(json_encode(simplexml_load_string($file)));
Note: I have modified my answer base on your comment.
You can try this way to clean out the url:
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>'GET',
)
);
$context = stream_context_create($opts);
$url= "http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?";
$query = array(
"token" =>$_GET['token'],
"agencyName"=>$_GET['agency'],
"stopName"=>$_GET['stopname']
);
$url = $url.http_build_query($query);
$url = rawurldecode($url);
print_r($url);
$file = file_get_contents($url, false, $context);
echo(json_encode(simplexml_load_string($file)));
?>

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);
}

Error while parsing xml file with php

I want to parse this page with php. I wrote this code, but it gives me an error - Invalid argument supplied for foreach()
$opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$context = stream_context_create($opts);
$data = file_get_contents('http://cbr.ru/scripts/XML_daily.asp',false, $context);
$xml = simplexml_load_string($data);
foreach($xml->valcurs->valute as $val){
echo "<p>".$val->attributes()->numcode."</p>";
}
Try this
foreach($xml->Valute as $val){
echo "<p>".$val->NumCode."</p>";
}
Might be the header then:
$opts = stream_context_create(array('http' => array('header' => 'Accept:
application/xml')));
Still think you shouldn't grab attributes() though:
foreach($xml->ValCurs->Valute as $val) {
echo "<p>".$val->NumCode."</p>";
}

php program works locally but not on server

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.

Categories