I am having a problem with PHP's file_get_contents command.
$url = "http://api.rememberthemilk.com/services/rest/".$format.$auth_token.$filter."&api_sig=".$md5.$apikey.$method;
$content = file_get_contents($url);
$array = json_decode($content, true);
$taskname = $array['rsp']['tasks']['list']['taskseries']['name'];
$duedate = $array['rsp']['tasks']['list']['taskseries']['task']['due'];
($format, $auth_token, $filter, $md5, $apikey, $method are already defined in the script)
When I try to run this code this error is returned:
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad request for line 101
line 101 = $content, = file_get_contents($url);
How to fix this? Thanks!!!
This url does not look great.
http://api.rememberthemilk.com/services/rest/?format=json&auth_token=AUTH_TOKEN&filter=dueWithin:"3 days of today"&api_sig=API_SIG&api_key=API_KEY&method=rtm.tasks.getList
Encode the tokens as follows:
$filter = 'filter='.urlencode( 'dueWithin:"3 days of today"' );
Use urlencode().
Try printing the URL after concatenating the variables. Then paste the URL into the address bar of your browser and see what comes back. Because it's a web service call, your browser might not know what to do with the response. In that case you might get additional information using the command-line user agent "curl", e.g.
curl -v 'http://some-url'
curl is built in to Macs and other *nix machines and is also available for Windows.
Related
I am using google currency conversion API in php by using file_get_content but unable to get output because of getting error ,so how to convert any currency by using following API in Php.
<?php
function convertCurrency($amount, $from, $to)
{
$url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to";
$data = file_get_contents($url);
preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
return $converted[1];
}
echo convertCurrency(1500, 'USD', 'INR');
?>
Getting error like this
Message: file_get_contents(http://www.google.com/finance/converter?a=1500&from=USD&to=INR): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
function thmx_currency_convert($amount){
$url = 'https://api.exchangerate-api.com/v4/latest/USD';
$json = file_get_contents($url);
$exp = json_decode($json);
$convert = $exp->rates->USD;
return $convert * $amount;
}
echo thmx_currency_convert(9);
A Bit Late, but it might help Some One,
As Benjamin said
You're not calling an actual API, you're scraping a web page, which means that:
you're most likely violating Google's TOS
you're more likely to get rate-limited (or be detected as abuse and be blacklisted) at some point if you're fetching this page too often
The Code Snippet
$url = "https://www.google.com/search?q=INR+to+USD";//Change Accordingly
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$data = explode("1 Indian Rupee = ",$result);//Change Accordingly
$one_inr_rate_to_usd = (float) substr($data[1],0,7);
I already answered a very similar question just a few days ago (the code was pretty much the same as yours).
I encourage you to read my answer:
You're not calling an actual API, you're scraping a web page, which means that:
you're most likely violating Google's TOS
you're more likely to get rate-limited (or be detected as abuse and be blacklisted) at some point if you're fetching this page too often
This is probably what you encountered here. You've most likely been blacklisted.
Solution: use a proper API such as OpenExchangeRates.
I'm trying to scrape data from some websites. For several sites it all seems to go fine, but for one website it doesn't seem to be able to get any HTML. This is my code:
<?php include_once('simple_html_dom.php');
$html = file_get_html('https://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=' . $_POST['data']);
echo $html; ?>
I'm using ajax to fetch the data. When I log the returned value in my js it's completely empty.
Could it be due to the fact that this website is running on https? And if so, is there any way to work around it? (I've tried changed the url to http, but I get the same result)
Update:
If I var_dump the $html variable, I get bool(false).
My PHP error log says this:
[27-Feb-2014 22:20:50 Europe/Amsterdam] PHP Warning: file_get_contents(http://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=tarmogoyf): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
in /Users/leondewit/PhpstormProjects/Magic/stores/simple_html_dom.php on line 75
It's your user agent, file_get_contents doesn't send one by default, so:
$url = 'http://www.magiccardmarket.eu/?mainPage=showSearchResult&searchFor=tarmogoyf';
$context = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla compatible')));
$response = file_get_contents($url, false, $context);
$html = str_get_html($response);
echo $html;
I want to read and parse the xml data from an url.My url is:"http://xml.gamebookers.com/sports/bandy.xml".I can access xml data from browser.However,when i attempt to read it by using php it doesnt work.It errors like this:
Warning: file_get_contents(http://xml.gamebookers.com/sports/bandy.xml): failed to open stream: Connection timed out in
How can i fix this?Any comments on this?
Thanks in advance..
Please see here for an answer:
This error is most likely connected to
too many (HTTP) redirects on the way
from your script to the file you want
to open. The default redirection
limit should be 20. As 20
redirects are quite alot there could
be some error in the filename itself
(causing e.g. the webserver on the
other end to do some
spellcheck-redirects) or the other
server is misconfigured or there are
some security measures in place or...
If you feel the need to extend the 20
redirects you could use a stream
context.
$context = array(
'http'=>array('max_redirects' => 99)
);
$context = stream_context_create($context);
// hand over the context to fopen()
$data = file_get_contents('http://xml.gamebookers.com/sports/bandy.xml', false, $context);
// ...
Please see:
Streams
stream_context_create()
HTTP context options
Try the snippet:
$request_url = 'http://xml.gamebookers.com/sports/bandy.xml';
$xml = simplexml_load_file($request_url) or die("feed not loading");
/*
Then just parsing out child node of your xml.
for example
*/
foreach($xml->children() as $child)
{
echo $child->getName().": ".$child."";
}
Hope this help
PS: Open your PHP.INI and look for
allow_url_fopen = On // make sure it is ON
I noticed there was a question somewhat similar to mine, only with c#:link text.
Let me explain: I'm very new to the whole web-services implementation and so I'm experiencing some difficulty understanding (especially due to the vague MediaWiki API manual).
I want to retrieve the entire page as a string in PHP (XML file) and then process it in PHP (I'm pretty sure there are other more sophisticated ways to parse XML files but whatever):
Main Page wikipedia.
I tried doing $fp = fopen($url,'r');. It outputs: HTTP request failed! HTTP/1.0 400 Bad Request. The API does not require a key to connect to it.
Can you describe in detail how to connect to the API and get the page as a string?
EDIT:
The URL is $url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main Page';. I simply want to read the entire content of the file into a string to use it.
Connecting to that API is as simple as retrieving the file,
fopen
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$fp = fopen($url, 'r');
while (!feof($fp)) {
$c .= fread($fp, 8192);
}
echo $c;
file_get_contents
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$c = file_get_contents($url);
echo $c;
The above two can only be used if your server has the fopen wrappers enabled.
Otherwise if your server has cURL installed you can use that,
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$c = curl_exec($ch);
echo $c;
You probably need to urlencode the parameters that you are passing in the query string ; here, at least the "Main Page" requires encoding -- without this encoding, I get a 400 error too.
If you try this, it should work better (note the space is replaced by %20) :
$url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$str = file_get_contents($url);
var_dump($str);
With this, I'm getting the content of the page.
A solution is to use urlencode, so you don't have to encode yourself :
$url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=' . urlencode('Main Page');
$str = file_get_contents($url);
var_dump($str);
According to the MediaWiki API docs, if you don't specify a User-Agent in your PHP request, WikiMedia will refuse the connection with a 4xx HTTP response code:
https://www.mediawiki.org/wiki/API:Main_page#Identifying_your_client
You might try updating your code to add that request header, or change the default setting in php.ini if you have edit access to that.
I've got a simple php script to ping some of my domains using file_get_contents(), however I have checked my logs and they are not recording any get requests.
I have
$result = file_get_contents($url);
echo $url. ' pinged ok\n';
where $url for each of the domains is just a simple string of the form http://mydomain.com/, echo verifies this. Manual requests made by myself are showing.
Why would the get requests not be showing in my logs?
Actually I've got it to register the hit when I send $result to the browser. I guess this means the webserver only records browser requests? Is there any way to mimic such in php?
ok tried curl php:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "getcorporate.co.nr");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
same effect though - no hit registered in logs. So far it only registers when I feed the http response back from my script to the browser. Obviously this will only work for a single request and not a bunch as is the purpose of my script.
If something else is going wrong, what debugging output can I look at?
Edit: D'oh! See comments below accepted answer for explanation of my erroneous thinking.
If the request is actually being made, it would be in the logs.
Your example code could be failing silently.
What happens if you do:
<?PHP
if ($result = file_get_contents($url)){
echo "Success";
}else{
echo "Epic Fail!";
}
If that's failing, you'll want to turn on some error reporting or logging and try to figure out why.
Note: if you're in safe mode, or otherwise have fopen url wrappers disabled, file_get_contents() will not grab a remote page. This is the most likely reason things would be failing (assuming there's not a typo in the contents of $url).
Use curl instead?
That's odd. Maybe there is some caching afoot? Have you tried changing the URL dynamically ($url = $url."?timestamp=".time() for example)?