get content of search result with file_get_content php - 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');

Related

Sending Image via Telegram-Bot API with PHP

I have this function
function suspendido($chat_id,$foo)
{
$TOKEN = "blablalbal";
$TELEGRAM = "https://api.telegram.org:443/bot$TOKEN";
$url. = "https://zrabogados-pruebas.xyz/bot/404.png";
$query = http_build_query(array(
'chat_id'=> $chat_id,
'photo'=> $url,
'text'=> $foo,
'parse_mode'=> "HTML", // Optional: Markdown | HTML
));
$response = file_get_contents("$TELEGRAM/sendMessage?$query");
return $response;
}
I try to sending an Image without using curl, tried to use file_get_contents but nothing works. Is something missing?.
Apparently, there is some problem with telegram servers.
If you put some image attributes into url it works.
function suspendido($chat_id,$foo)
{
$TOKEN = "blablalbla";
$TELEGRAM = "https://api.telegram.org:443/bot$TOKEN";
$url = "https://zrabogados-pruebas.xyz/bot/404.png?center=140.50,36.15&width=1024&height=576";
$query = http_build_query(array(
'chat_id'=> $chat_id,
'photo'=> $url,
'text'=> $foo,
'parse_mode'=> "HTML", // Optional: Markdown | HTML
));
$response = file_get_contents("$TELEGRAM/sendMessage?$query");
return $response;
}
I know its just silly but it works that way if you send the image url without this then you will receive a 400 error.
You used sendMessage method, And this method isn't accept photo parameter.
And $url shouldn't be concatenated with another link.
To send photo use sendPhoto method like this:
<?php
function suspendido($chat_id, $url, $foo)
{
$TOKEN = "<bot_token>";
$TELEGRAM = "https://api.telegram.org:443/bot$TOKEN";
$url = "https://zrabogados-pruebas.xyz/bot/404.png";
$query = http_build_query(array(
'chat_id'=> $chat_id,
'photo'=> $url,
'text'=> $foo,
'parse_mode'=> 'HTML' // Optional: Markdown | HTML
));
# Use sendPhoto here, Not sendMessage
$response = file_get_contents("$TELEGRAM/sendPhoto?$query");
return $response;
}

Streaming a response in CakePHP

I want CakePHP to stream a download to the browser. The content of the stream is served via an API.
So, CakePHP makes a request to that API, gets a response with a file and must stream this response to the browser.
This is what I got so far:
public function getDownload() {
// do other things
$http = new Client([
'headers' => [
'accept' =>'application/octet-stream'
]
]);
$response = $http->get($this->url,[]);
// first try
// $stream = new CallbackStream(function () use ($response) {
// return $response;
// });
// $response = $response->withBody($stream);
// second try
// $stream = new CallbackStream($http->get($this->url,[])->getData());
// $response = $response->withBody($stream);
return $response;
}
With this setup I can download small files. The reason I need a stream is, because the API could send files up to 10GB. My guess is, that with $http->get CakePHP stores the whole response in memory. Thats why I'm getting a memory exhausted error.
I know I'm lacking a bit of understanding here. Any help is appreciated :)
Finally I found a solution:
public function getDownload($url) {
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"accept: application/octet-stream\r\n"
)
);
$context = stream_context_create($opts);
$response = new Response();
$file = fopen($url, 'r',false, $context);
$stream = new CallbackStream(function () use ($file) {
rewind($file);
fpassthru($file);
fclose($file);
});
$response = $response->withBody($stream);
return $response;
}

loadHTML only returns javascript, it doesn't returns source

I am trying to get HTML content of the page, but when I do that, my file_get_contents only returns piece of JS.
when I use just file_get_contents I can see the content but I am trying to use loadHTML as well. So using the function like below.
function getContent(String $url): \DOMDocument
{
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"Content-Type: text/html; charset=utf-8"
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url,false,$context);
$parser = new \DOMDocument();
$parser->loadHTML($result = mb_convert_encoding($result,'HTML-ENTITIES', 'ASCII, JIS, UTF-8, EUC-JP, SJIS'));
return $parser;
}
But this time, I am having DOMDocument::loadHTML(): Tag header invalid in Entity So tried to overcome this problem I used libxml_use_internal_errors(true); this but, then I am just returning JS part of page...
How can I overcome issue?
The links is here: https://lions-mansion.jp/MF161037/

Get Links with simple html dom

I'm trying to get links from the site.
"http://www.perfumesclub.com/es/perfume/mujer/c/"
For this use "user-agent" in simple html Sun.
But I get this error ..
Fatal error: Call to a member function find() on string in C:\Users\Desktop\www\funciones.php on line 448
This is my code:
Thanks^^
$url = 'http://www.perfumesclub.com/es/perfume/mujer/c/';
$option = array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
)
);
$context = stream_context_create($option);
$html = new simple_html_dom();
$html = file_get_contents ($url, false, $context);
$perfumes = $html->find('.imageProductDouble'); --> this is line 448
foreach($perfumes as $perfume) {
// Get the link
$enlaces = "http://www.perfumesclub.com" . $perfume->href;
echo $enlaces . "<br/>";
}
wrap your file_get_contents in str_get_html function
// method 1
$html = new simple_html_dom();
$html->load( file_get_contents ($url, false, $context) );
// or method 2
$html = str_get_html( file_get_contents ($url, false, $context) );
you're creating a new dom and assigning it to the variable $html, than reading the url returning the string and setting it to $html, thus overwriting your simple_html_dom instance, so when your invoking the find method you have a string instead of an object.
$html is a string after the call to file_get_contents. Try
$html = file_get_html($url);
OR use
$html = str_get_html($html);
after the call to file_get_contents.

HTTP POST from PHP, without cURL

I am using the example function given in this post:
<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
?>
I also tried a similar approach using file_get_contents(), like this:
$options = array(
'http'=>array(
'method'=>"POST",
'header'=>
"Accept-language: en\r\n".
"Content-type: application/x-www-form-urlencoded\r\n",
'content'=>http_build_query(
array(
'arg1'=>'arg_data_1',
'oper'=>'get_data',
'arg2'=>'arg_data_2',
'id_number'=>'7862'
),'','&'
)
));
$context = stream_context_create($options);
$refno = file_get_contents('/path/to/script/script.php',false,$context);
var_dump($refno);
With both these scripts, the response from the server script is the same, and it is the TEXT of the script.php. The code of the server script is never begin executed, and the text content (the PHP code) of the script is being returned to the original script.
A little strange that it doesn't return all the text, but just certain pieces... I tried making a test script (test.php) that just contains:
<?php
echo '{"a":1,"b":2,"c":3,"d":4,"e":5}';
?>
but that doesn't return anything from the POST request, so I didn't include that. The script.php is a must longer script that does a lot of logic and MySQL queries then returns a JSON object.
The desired output will be to have the PHP code execute and return a JSON object (the way it works with ajax).
What am I doing wrong?
You are trying access script localy.
You must call it like any other external script like
$refno = file_get_contents('http://yourhost/path/to/script/script.php',false,$context);

Categories