PHP Reading currency quotes when "allow_url_fopen=0" - php

I need a fresh EURUSD quote to show my product prices in a website.
I'm trying to read the quote from Yahoo finance with the following code:
$eurUsd = 1.3700; // Default value
$yahooUrl = 'http://finance.yahoo.com/d/quotes.csv?f=l1&s=EURUSD=X';
$handle = fopen($yahooUrl, 'r');
if ($handle) {
$result = fgetcsv($handle);
fclose($handle);
$eurUsd = $result[0];
}
This code works fine in my testing environment but not in my website since I have "allow_url_fopen=0". I do not want to change it for a security reason.
The request I'm using returns a string like:
1.3715
Do you know how I can read the string in other way?
Do you know an alternative way to access the EURUSD quote?
EDIT
Thanks to Epik's code I tried to use curl, but without success with Yahoo.
Finally I rid off Yahoo and succeed with Rate Exchange JSON/JSONP APIs
A working solution:
$url = 'http://rate-exchange.appspot.com/currency?from=EUR&to=USD';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$eurUsd = json_decode($result)->{'rate'};

$ch = curl_init();
$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1&s=EURUSD=X';
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

Related

Bit Coin API for bit coin rate

I am using following code to get bitcoin price it is working off line but not working on server.
$api = "http://blockchain.info/ticker";
$json = file_get_contents($api);
$data = json_decode($json, TRUE);
$rate = $data["USD"]["15m"];
$symbol = $data["USD"]["symbol"];
echo $rate.$symbol;
?>
Solve when i use curl
$url = "http://blockchain.info/ticker";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$data = json_decode($result, TRUE);
$rate = $data["USD"]["15m"];
$symbol = $data["USD"]["symbol"];
echo $rate.$symbol;
thanks to all for support..
This method is not secure to perform a request to other server. You should use curl instead of file_get_contents. Btw, if still you want to keep this approach you can enable it in php.ini setting allow_url_fopen to 1

Wikipedia Api connect via php curl

I have a task: get by inputed keyword Wikipedia article, save it to database and then make a search inside them.
The problem is: how to access api and retrieve data from wikipedia, I've tried this url (at the begining i've tried json format):
$url = 'https://en.wikipedia.org/w/api.php?action=query&titles=Dog&prop=revisions&rvprop=content&format=xml';
and this php code:
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$res = curl_exec($ch);
if (!$res) {
echo 'cURL Error: '.curl_error($ch);
}
var_dump($res);
but nothing happend. Is it possible to access data with curl?
At the end one code worked with url above:
ini_set('user_agent','TestText');
$xmlDoc = new \DOMDocument();
$xmlDoc->load($url);
echo($xmlDoc->saveXML());
and then I get the text like this
{{about|the domestic dog|related species known as "dogs"|Canidae|other
uses|Dog (disambiguation)|}} {{Redirect|Doggie|the Danish
artist|Doggie (artist)}} {{pp-semi-indef}} {{pp-move-indef}} {{Taxobox
| name = Domestic dog | fossil_range = {{Fossil
range|0.033|0}}[[Pleistocene]] – [[Recent]] |
How can I handle it to be prettier (text with paragraphes or at liest plain text)?
So, There are two questions:
1. Is it possible to access wiki data with php curl and how I should improve my code?
2. How do I make wiki xml code prettier?
My question about code, especially about curl. Why it doesn't work?
And also, answer to another question says only about wikipedia api urls. By only changing url I can't solve problem.
I've found the solution, CURLOPT_SSL_VERIFYPEER was needed:
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&explaintext=&titles=Dog';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$res = curl_exec($ch);
//$json_data = mb_substr($res, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
curl_close($ch);
$json = json_decode($res);
$content = $json->query->pages;
$wiki_id = '';
foreach ($content as $key => $value) {
$wiki_id = $key;
}
echo $content = $content->$wiki_id->extract;

PHP Using cURL and GET request in URL

I am using cURL instead of file_get_contents, which is working fine on the URL until I used a GET request variable in place of the city on the URL below.
Using cURL on the following: (Works)
$url = 'http://www.weather-forecast.com/locations/London/forecasts/latest';
This works fine, however when substituting 'London' with a variable $city:
URL: example.com/weather.php?city=London
$city = $_GET['city'];
$city = ucwords($city);
$city = str_replace(" ", "", $city);
$url = 'http://www.weather-forecast.com/locations/".$city."/forecasts/latest';
I get an error: The page you are looking for doesn't exist (404)
What am I doing wrong in my cURL function? This seems to work perfectly with file_get_contents, is there something I am missing?
cURL function
function curl_get_contents($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$contents = curl_get_contents($url);
echo $contents;
Please replace double quotes with single quotes in url,
$url = 'http://www.weather-forecast.com/locations/'.$city.'/forecasts/latest';

why Google Search Api not getting indexed page result?

Developed a Wordpress Plugin to Index Pages in Google.The Pages getting indexed,i checked it by site:url in google search,it showing result.but not getting result while check through my code.
here is my code
$ip_server=$_SERVER['SERVER_ADDR'];
$site_url=site_url();
$query =urlencode('http://helpfultherapy.com/best-therapy-for-you/');
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q=site:$query&userip=$ip_server";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $site_url);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
$json->responseData-> // results is getting null here
The code is success for me, make sure curl extension is enabled on your server,
Properly escape the below line of code:
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:".$query."&userip=".$ip_server;
try with replacing the $url in your code....
Your code works perfectly from my end after adding this option for https handling.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Second cURL is not working, the destination is a servlet and is not working

<?php
in this part I provide a latitude a longitude to reverse geocode.
$ch = curl_init("http://maps.google.com/maps/api/geocode/json?latlng=39.784268,-98.393555&sensor=false");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);
$geoOutput = json_decode($return, true);
$data = current($geoOutput);
echo $data;
After that I want to pass the content to a servlet but for example purposes I embedded the street and zipcode.
The generated url is like the following, but if you run the url will not happen anything in curl. But if you copy paste the code in the browser will succed.
$urlse="http://136.145.116.30:8080/AccidentDetailsInserter?latitude=18.209&longitude=-67.139&frontal_damage=1&left_damage=1&right_damage=0&back_damage=0&smoke_detected=0&flipped=0&maxforce=7&roll=0&pitch=0&yaw=0&device_id=47&street=Calle Caobos&city= Mayagüez&state=PR&zip= 00680";
echo $urlse;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$urlse);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
here the output is blank
?>
I tried to write to specify the port to 8080 but nothing.
Your second URL contains spaces and other unescaped characters that should be urlencoded. If you paste it in the browser the browser fixes that for you. CURL doesn't.
instead of street=Calle Caobos or other such parameters, you should send street=Calle%20Caobos. just use urlencode() on all parameters you are building.
Edit: there were a few things missing here, here is some working code:
$urlse="http://136.145.116.30/AccidentDetailsInserter?latitude=18.209&longitude=-67.139&frontal_damage=1&left_damage=1&right_damage=0&back_damage=0&smoke_detected=0&flipped=0&maxforce=7&roll=0&pitch=1&yaw=0&device_id=47&street=CalleCaobos&city=SanJuan&state=PR&zip=00926";
echo $urlse;
$ch = curl_init();
$timeout = 10;
curl_setopt($ch,CURLOPT_URL,$urlse);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_PORT, 8080);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

Categories