Find me code below just help me
<?php
$result=file_get_contents('http://finance.google.com/finance/info?client=ig&q=DHAMPURSUG');
$res=json_decode(result);
print_r($res);
?>
Try using basic cURL Post.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://finance.google.com/finance/info?client=ig&q=DHAMPURSUG");
curl_setopt($ch, CURLOPT_POST, 1);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);
On your server, allow_url_fopen may be set to false, which would prevent file_get_contents() from fetching a URL (see the manual).
To figure out whether this is the case, run:
var_dump(ini_get('allow_url_fopen'))
If 0 or false is output on your production server, then you've found your problem. 2 possible fixes:
Edit the INI file
One fix is to change your php.ini file to set this setting to 1 or On. To find the ini file that governs your script, execute:
echo php_ini_loaded_file();
Go edit that file, then restart the web server.
Use cURL instead
An alternative is to use cURL to connect to google and fetch the info you need:
$options = [
CURLOPT_URL => 'http://finance.google.com...?client=ig&q=DHAMPURSUG',
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => true,
];
$ch = curl_init();
curl_setopt_array($ch,$options);
$result = curl_exec($ch);
curl_close($ch);
if($result===false) die(curl_errno($ch));
$result now holds the response.
Addendum
Another possibility (thanks Hanky Panky) is that the json_decode function is not available on your production server. To test this, run the line below to see whether it prints true
var_dump(function_exists('json_decode'));
You can upload the htaccess file in the root directory.
I have a cURL request in my code which works fine when running locally:
$url = "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$locale = json_decode($response);
and returns a JSON as expected. Our production system is on Google App Engine, however, where I get the website version for a browser rather than the JSON.
I can get this cURL request to work if I change
google_app_engine.enable_curl_lite = "1"
in the php.ini in the root directory of my project to
extension = "curl.so"
but Google's documentation insists the former is to be used on production. Additionally, using the latter breaks things like Monolog's SlackHandler.
Is there a way to get the JSON from this cURL request while still using Google's "cURL Lite"?
From the ipinfo.io documentation:
"We do a little bit of magic on the server to determine if we should send the JSON response or the webpage. We usually get it right, but if you're seeing the webpage instead of the JSON (or want to check the JSON output in a browser) you can force the JSON response by adding /json to the end of the URL"
Adding /json to the end of the URL worked for me in this case, but I wanted a more general solution. Since Google's cURL Lite uses their URL Fetch in the background, ipinfo.io's "magic" is somehow getting confused. I found that by specifying the Accept header then the /json addition wasn't required. In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Thanks to the folks on the #php channel of NashDev Slack for helping me on this one!
I'm new to programming and I don't know how to set up a normal PHP extension like cURL. I've installed PEAR packages before but that's all. I think what I'm trying to do is very simple - just getting Facebook's linter to lint my URL upon a page reload on my site. The code Facebook suggests is simply this:
curl https://developers.facebook.com/tools/lint/?url={YOUR_URL}&format=json
Is this supposed to just work if I throw it inside of <?php ?> tags, or is Facebook not assuming that I use PHP? Let's say my site's URL is http://www.example.com - how should this code look in a PHP file? And how am I supposed to install the cURL library? Sorry for being clueless! ;)
This should help you installing cURL: How to install PHP/CURL?
You can use a code similar to this to get the page:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://developers.facebook.com/tools/lint/?url=" . urlencode ( 'http://www.example.com/' ) . "&format=json");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
When I run curl on a particular url, the site stops responding and doesn't generate an error, despite my having set error reporting to on. I've tried setting the curl timeouts to low values, and it generates an error then, so I know its not timing out.
The main thing I want to know is, how could that even happen, and how can I figure out why?
The url I'm trying to access is a call to the Factual api, and the url I'm using here
(http://api.factual.com/v2/tables/bi0eJZ/read?api_key=*apikey*&filters={"category":"Automotive","$loc":{"$within":{"$center":[[41,-74],80467.2]}})
Works when you put it in a browser. The php script works as intended if you change the latitude and longitude to essentially any other values.
error_reporting(E_ALL);
ini_set('display_errors', '2');
$url="http://api.factual.com/v2/tables/bi0eJZ/read?api_key=*apikey*&filters={\"category\":\"Automotive\",\"\$loc\":{\"\$within\":{\"\$center\":[[41,-74],80467.2]}},\"website\":{\"\$blank\":false}}";
Echo "\n\n1";
$ch = curl_init($url);
Echo 2;
curl_setopt($ch, CURLOPT_HEADER, 0);
Echo 3;
curl_setopt($ch, CURLOPT_POST, 1);
Echo 4;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,30);
Echo 5;
$output = curl_exec($ch) or die("hhtrjrstjsrjt".curl_error($ch));
Echo 6;
curl_close($ch);
Echo "out: ".$output;
It looks like you have some mistakes in your PHP configuration file.
To fix your errors you must edit your php.ini file.
For displaying errors in development mode, change the error_reporting value to E_ALL.
error_reporting=E_ALL
Then you have to enable the cURL extension.
To enable it in your php.ini, yous have to uncomment the following line:
extension=php_curl.dll
Once you edited this values, don't forget to restart your webserver (Apache or Nginx)
Also I agree with my colleagues, you should url_encode your JSON string.
From my point of view the code should be:
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
$apiKey = '*apikey*';
$filters = '{"category":"Automotive","$loc":{"$within":{"$center":[[41,-74],80467.2]}},"website":{"$blank":false}}';
$params = '?api_key=' . $apiKey . '&filters=' . url_encode($filters);
$url = 'http://api.factual.com/v2/tables/bi0eJZ/read';
$url .= $params;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$output = curl_exec($ch) or die("cURL Error" . curl_error($ch));
curl_close($ch);
echo "out: " . $output;
EDIT:
Another approach could be to use the Official PHP driver for the Factual API:
Official PHP driver for the Factual API
It provides a Debug Mode with a cURL Debug Output and a Exception Debug Output.
Your url is not url_encoded as CURL is an external application escaping is necessary your browser will auto url_encode params on the URL however you could be breaking curl on the server and it is halting.
Try changing this:
$url="http://api.factual.com/v2/tables/bi0eJZ/read?api_key=*apikey*&filters={\"category\":\"Automotive\",\"\$loc\":{\"\$within\":{\"\$center\":[[41,-74],80467.2]}},\"website\":{\"\$blank\":false}}";
to:
$url_filters = '{"category":"Automotive","$loc":{"$within":{"$center":[[41,-74],80467.2]}},"website":{"$blank":false}}';
$url="http://api.factual.com/v2/tables/bi0eJZ/read?api_key=*apikey*&filters=".urlencode($url_filters);
However i do have some question is your call correct? the key of literlal "$loc" is that correct?
Updated to remove the need to backslash everything single quotes don't support variable replace and will allow double quotes without escaping them
For future benefit:
Use urlencode for query parameters as your filters parameter
contains many characters that are not safe/valid for URLs
Use curl_getinfo() to see information about http_code and
other useful information.
In my case the curl failure was due to the fact that the hosting provider had replaced Apache with Litespeed, and litespeed was terminating the process during the curl request.
The php.ini settings didn't fix this as lsphp was getting terminated at 30 seconds every time.
We had a long chat, and I convinced them their server was actually broken (eventually).
To make this clearer for other people who might have a similar or related problem:
My PHP script was running, and no error was being logged in PHP because the entire PHP process, including the error logger was being terminated by the web server.
this code is just for testing, just modify your code according to mine
<?php
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko)
Chrome/8.0.552.224: Safari/534.10'; // notice this
$url="http://api.factual.com/v2/tables/bi0eJZ/read?api_key=*apikey*&filters={\"category\":\"Automotive\",\"\$loc\":{\"\$within\":{\"\$center\":[[41,-74],80467.2]}},\"website\":{\"\$blank\":false}}";
$ch = curl_init(); // notice this
curl_setopt($ch, CURLOPT_URL, $url); // notice this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$contents = curl_exec($ch);
echo $contents;
?>
I have worked with CURL calls from vanilla PHP before. The CURL call is a part of the curl library. As other users have suggested, the url_encode($url) function would prepare the string for use by this particular class. If you do not run this, then you could pass in URLs which would break the handlers (e.g. by having invalid characters or URL syntax).
On top of this, it seems that you are trying to pass a JSON object directly into the URL of a page. I do not believe it is best practice to work with JSON packages in a curl call like this. If this is what you are looking to do, see this page: How do I POST JSON data with cURL?
the fact that the script stop doing anything at all you can found it in here:
http://php.net/manual/en/function.set-time-limit.php
excerpt:
The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.
so basically, curl blocks all the php script and basically the only thing that is actually running is curl, so if it blocks forever, your site will no respond, thats why you need to use timeouts...
as how to avoid it, just use timeouts...
I use the following command in some old scripts:
curl -Lk "https:www.example.com/stuff/api.php?"
I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.
What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.
I suspect something in the order of :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// What goes here so that I just get the Location and nothing else?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
The goal being $response = the data from the api “OK=1&ect”
Thank you
I'm a little confused by your comment:
// What goes here so that I just get the Location and nothing else?
Anyway, if you want to obtain the response body from the remote server, use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
If you want to get the headers in the response (i.e.: what your comment might be referring to):
curl_setopt($ch, CURLOPT_HEADER, 1);
If your problem is that there is a redirection between the initial call and the response, use:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);