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.
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 am trying to access a file at this url: http://www.myurl.com/伊勢/image.jpg.
The urls are predefined and there is no specific format or consistency.
The basic curl function I am using is fine for downloading images from myurl.com, but not when there are Japanese characters contained in the url. I have tried sanitising the url in various ways (such as urlencode, filter_var, and mb_convert_encoding), with no success.
If I visit the url directly from the browser, it's fine - so the only problem that I can't resolve is the handling of non-ASCII (Japanese) characters in the curl function.
My question is - how can this be resolved? Is there a curl option that can be included in the function in order to read the url as a browser will?
If I visit the url directly from the browser, it's fine
That means your browser encode "伊勢" (like %E4%BC%8A%E5%8B%A2) and send request in background. But still keep the look in your browser address box.
My suggestion is use http debugger, like "firebug" in firefox or "developer tools" in chrome.
Check the "network" tab and find the REAL request parameters in its detail page. Then you can find what your browser sent.
Hope this helpful.
Nothing special.
I have created a php file in UTF-8 (using notepad's save as encoding UTF-8 ):
<?php
$url = 'http://rp.postcontrol.ru/伊勢.txt';
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
if ( $result = curl_exec($ch) )
{
echo $result;
}
else
echo "cURL error: ".curl_error($ch);
curl_close( $ch );
You may take the PHP file at http://rp.postcontrol.ru/eddz.php.txt
It works for me and returns (伊勢.txt is in UTF-8 too):
おはようございます eddz さん.
Append the path parameter as a url encoded string and it will work.
ex:
$url = 'http://rp.postcontrol.ru/';
$filename = urlencode("伊勢.txt");
$url .= $filename;
I am trying to read Raven SEO Tools API. It is a REST API and currently it is serving the data backup as an XML (or JSON if I choose) when I just request the URL through a web browser. What is the best method to get the response from their server into my own PHP script for me to then play around with.
Any help much appreciated
Cheers
If you only needs to retrieve a URL and parse its info. The easiest way is curl/JSON combination. Note that parsing JSON is faster than parsing XML.
http://www.php.net/manual/en/function.curl-exec.php
http://www.php.net/manual/en/function.json-decode.php
Something simple as:
$url = "http://api.raventools.com/api?key=B1DFC59CA6EC76FF&method=domains&format=json";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
print_r(json_decode($json));
But if you need to call other methods from this API such as DELETE/PUT, etc. Then to have a REST client in PHP is more elegant solution. A comparison on those clients can be found in PHP REST Clients
I founded this code specifically for Raven API https://github.com/stephenyeargin/raventools-api-php
Sample code:
require 'path/to/raventools-api-php/raventools-api-php.class.php';
$Raven = new RavenTools( 'B1DFC59CA6EC76FF' );
$method = 'domains';
$options = array('format'=> 'json');
$responseString = $Raven->getJSON($method, $options);
print_r(json_decode($responseString));
cUrl
cUrl is a command line tool for getting or sending files using URL syntax.
curl -o example.html www.example.com
file_get_contents
<?php
$homepage = file_get_contents('http://www.example.com/api/parameters');
echo $homepage;
?>
Pecl's HTTPRequest class is a very nice client, I've been using it for a couple of Projects. http://pecl.php.net/package/pecl_http
Another pretty cool client is the Buzz client https://github.com/kriswallsmith/Buzz
It also plays nice with Symfony2 if that's of interest to you :)
You can use either one of them, but I think JSON is the easiest and more hassle-free, unless you use SimpleXML. The decision depends on the complexity of your data.
Given that the JSON returned by the API is valid you can convert it to an array or object by using PHP's json_decode() function.
<?php
# retrieve JSON from API here...
# i.e. it is stored in $data as a string
$object = json_decode($data);
$array = json_decode($data, true);
?>
In SimpleXML, it would be as follows:
<?php
$object = simplexml_load_string($data);
?>
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.
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)?