PHP file_get_contents/curl - getting different result than browser
try to test it using saving cookies to same directory where the script resides first
so set the cookies path like that
$cookie = "cookie.txt";
this code works with me and i got the page
<?php
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_page = curl_get_contents("http://www.nytimes.com/2014/01/26/us/politics/rand-pauls-mixed-inheritance.html?hp&_r=1");
echo $get_page;
?>
file_get_contents gets different file from google than shown in browser
Nothing really. The problem is that the page uses javascript and ajax to get contents. So, in order to get a "snapshot" of the page, you need to "run it". That is, you need to parse the javascript code, which php doesn't do.
Your best bet is to use an headless browser such as phantomjs. If you search, you find some tutorials explaining how to do it
NOTE
If all you're looking for is a way to retrieve raw data from the search, you might want to try to use google's search api.
file_get_contents and browser session
PHP runs serversided so it has its own session handling, there is no link to your browsers session. You can use cURL and options like CURLOPT_COOKIEJAR
to do this. With cURL you can login via PHP and keep the PHP session of the website you are requesting. You will find a bunch of examples in the cURL documentation I linked.
file_get_contents() not working with $_REQUEST variables
make sure you are getting value in your $query
variable and then make your requested url as
$query = $_REQUEST['query'];
$url="https://en.wikipedia.org/wiki/".$query;
then use file_get_contents() as
$result=file_get_contents($url);
PHP script behaves differently in browser
You're making a few (extremly wrong) assumptions in the following lines:
$whole = substr($price/$spot, 4, -13);
$dec = substr($price/$spot, 4, -12);
$price / $spot
is treated as a string and you assume it will be in the format
'0.0019XXXXXXXXXXXX' // 12 x's (unkown numbers)
What if Bitcoin is doing really bad and the rate exeeds 10 mBTC per USD? $price / $spot
will be something like:
'0.0108491827849201'; // (10.8 mBTC)
$whole = substr('0.0108491827849201', 4, -13); // Will be '0'
$dec = substr('0.0108491827849201', 4, -12); // Will be '08'
echo $whole . '.' . $dec . ' mBTC'; // Will echo '0.08 mBTC'
What if, due to rounding or accuracy (! this is what you're seeing in your server - most likely because your OSX is 64bit, your server 32bit or vice-versa !), the string-length of $price / $spot
is less than 18 characters:
'0.0019564521431';
$whole = substr('0.0019564521431', 4, -13);
// Meaning: start at position 4, stop at 13 characters counting from the end
// 13 characters from the end is here: '0.0019564521431'
// ^
// so the stop-position is before the start-position, resulting in an empty
// string. Same with $dec.
echo $whole . '.' . $dec . ' mBTC';
// Will echo empty-string . '.' . empty-string . ' mBTC': '. mBTC'
Long story short: never ever treat numbers as a string (unless you have no other options and you are fully aware of what you are doing). Following code will work, and will give the correct output:
echo number_format($price / $spot * 1000, 1);
// multiply by 1000: BTC to milli-BTC
// , 1: One digit after the dot
For a full explanation of number_format see: http://php.net/number_format
Related Topics
How to Destroy Session with Browser Closing in Codeigniter
Patterns for PHP Multi Processes
Converting Human-Friendly Date to Milliseconds
How to Select a Result from the Select2 Search Results
Protect the Excel File Using PHPexcel
What Is the Rfc Compliant and Working Regular Expression to Check If a String Is a Valid Url
Merge Two Png Images with PHP Gd Library
How to Set Base Url for All Pages of My Website
Getting Elements of a Div from Another Page (Php)
Zend Framework 2 - Removed Form Element Causes Validation to Fail
Nginx + PHP-Fpm 7.1 - 504 Gateway Time-Out
Google Map: Is a Lat/Lng Within a Polygon