I have this code :
$feed = 'urltorss';
$feeds = new SimpleXmlElement( file_get_contents($feed) );
$feed = 'http://www.businessweek.com/feeds/homepage.rss';
$feeds = new SimpleXmlElement( file_get_contents($feed) );
Now both urls don't have any special characters that require url encoding them, but the 2nd url has an .rss extension if it's related.
The first call works and i get a timeout on the 2nd when i try on my server but works perfectly on localhost.
This is the error i'm getting :
Severity: Warning
Message: file_get_contents('http://www.businessweek.com/feeds/homepage.rss')
[function.file-get-contents]:
failed to open stream: Connection timed out
Filename: controllers/mailsystem.php
Line Number: 36
Why is that?
First, check your path so
echo $feed; // just for debug
then check the content
$content = #file_get_contents($feed);
and in the end, get the XML
if( $content ) { $feeds = new SimpleXmlElement( file_get_contents($feed) ); }
Edit: file_get_contents for URLs will only work if you have allow_url_fopen = 1. To get the content of http://www.businessweek.com/feeds/homepage.rss you need a cURL function that gets the content. Something like http://davidwalsh.name/curl-download
Related
I'm a newbie and I have a url that contains Persian language characters.
For example this:
http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی-از-جنجال-پاسخ-مشایخی-به-مجیدی-و-حرفهای-عجیب-الویس-پریسلی-ایران
When I want to get the html source of that url, with this line of code:
$source = file_get_contents($url);
I get this error:
Warning: file_get_contents(http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی-از-جنجال-پاسخ-مشایخی-به-مجیدی-و-حرفهای-عجیب-الویس-پریسلی-ایران):
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request in C:\wamp\www\file.php on line 25
I wanted to solve this problem by using the urlencode, but it didn't work.
The urlencode output of that line becomes:
http%3A%2F%2Ftabnak.ir%2Ffa%2Fnews%2F577155%2F%D9%88%DB%8C%D8%AF%DB%8C%D9%88%DB%8C-%D8%AF%D8%B1%DA%AF%DB%8C%D8%B1%DB%8C-%D9%86%DB%8C%D8%B1%D9%88%D9%87%D8%A7%DB%8C-%D8%B3%DB%8C%D8%A7-%D9%88-%D9%BE%D9%86%D8%AA%D8%A7%DA%AF%D9%88%D9%86-%D8%AF%D8%B1-%D8%B3%D9%88%D8%B1%DB%8C%D9%87-%D8%A8%D8%A7-%D9%87%D9%85%D8%AF%DB%8C%DA%AF%D8%B1-%D9%88%DB%8C%D8%AF%DB%8C%D9%88%D9%87%D8%A7%DB%8C%DB%8C-%D8%A7%D8%B2-%D8%AC%D9%86%D8%AC%D8%A7%D9%84-%D9%BE%D8%A7%D8%B3%D8%AE-%D9%85%D8%B4%D8%A7%DB%8C%D8%AE%DB%8C-%D8%A8%D9%87-%D9%85%D8%AC%DB%8C%D8%AF%DB%8C-%D9%88-%D8%AD%D8%B1%D9%81%E2%80%8C%D9%87%D8%A7%DB%8C-%D8%B9%D8%AC%DB%8C%D8%A8-%D8%A7%D9%84%D9%88%DB%8C%D8%B3-%D9%BE%D8%B1%DB%8C%D8%B3%D9%84%DB%8C-%D8%A7%DB%8C%D8%B1%D8%A7%D9%86
Which is not a correct url address, and I can't get contents again.
What should I do?
Can you try this ? This way, you should be able to do the file_get_contents on the encoded url
$url = 'http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی-از-جنجال-پاسخ-مشایخی-به-مجیدی-و-حرفهای-عجیب-الویس-پریسلی-ایران';
$url = mb_convert_encoding($url, 'HTML-ENTITIES', "UTF-8");
$source = file_get_contents($url);
EDIT (TESTED THIS AND WORKS) :
Try this, maybe by encoding the part of the URL where there are arabic character, it might work :
$link = 'http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی-از-جنجال-پاسخ-مشایخی-به-مجیدی-و-حرفهای-عجیب-الویس-پریسلی-ایران';
$exploded = explode('/',$link);
$exploded[6] = urlencode($exploded[6]);
$urlimplode = implode($exploded,'/');
$source = file_get_contents($urlimplode);
echo $source;
In my case, where I wanted to pass a parameter with Get, I did as follows:
$message = "یک پیغام تست";
$link = "http://localhost:5000/myAPI_name?msg=".$message;
$exploded = explode('=',$link);
$exploded[1] = urlencode($exploded[1]);
$urlimplode = implode($exploded,'=');
$source = file_get_contents($urlimplode);
$source=json_decode($source,true);
As the content of $message is Persian, I had to perform encoding just for anything after =
I am trying to get details for bans on a Steam user account using the file_get_contents function in PHP. However, it keeps erroring and I have no idea why:
$detailURL = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=3C11B751BB59DEEE28D744C0A8BAC2EC&steamids=76561198078737264");
$profile = file_get_contents($detailURL);
$buffer = fopen("coOn4LxnQm/{$steamIDpost}.json", "w+");
fwrite($buffer, $profile);
fclose($buffer);
$steam = json_decode(file_get_contents("coOn4LxnQm/{$steamIDpost}.json"));
The error is as follows:
Warning: file_get_contents({ "players": [ { "SteamId": "76561198078737264", "CommunityBanned": false, "VACBanned": true, "NumberOfVACBans": 1, "DaysSinceLastBan": 309, "NumberOfGameBans": 0, "EconomyBan": "none" } ] }): failed to open stream: Invalid argument in C:\xampp\htdocs\addToList.php on line 18
If you have any idea, please help me. No idea what "failed to open stream: Invalid argument" means and googling and such did not help much.
Thanks.
After this line
$detailURL = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=3C11B751BB59DEEE28D744C0A8BAC2EC&steamids=76561198078737264");
$detailURL will now contain the result from the API (a JSON String)
If you have a look at this line:
$profile = file_get_contents($detailURL);
This line is doing a file_get_contents on a JSON String, and it has no sense.
I am using SOAP to get data from some server (data coming in .xml) . But sometimes SOAP server is down and I need to display some error message instead of :
Warning: simplexml_load_string(): Entity: line 2: parser error : Start tag expected, '<' not found in /var/www/class/data2.php on line 619 Warning: simplexml_load_string(): in /var/www/class/data2.php on line 619 Warning: simplexml_load_string(): ^ in /var/www/class/data2.php
My code is:
$client = new SOAPClient ( 'link.wsdl' ); // initiate new SoapClient
$password ['_'] = 'PASSWORD'; // password for authenticate_user function in SoapHeader
$encoded = new SoapVar ( $password, SOAP_ENC_OBJECT ); // make SoapVariable out of $password
$header = new SoapHeader ( 'http://soapinterop.org/echoheader/', 'authenticate_user', $encoded ); // put authenticate_user method, and password in header
$client->__setSoapHeaders ( $header ); // set SoapHeader
$response = $client->get_details ($this->vin); // calling get_details with the vin given in the form field
$xml = simplexml_load_string ( $response ); // converting the response string to xml
$json = json_encode ( $xml ); // converting to an array in to easy steps (step 1)
$array = json_decode ( $json, TRUE ); // step 2
What I want:
Replace Warning message with something like: "This service is temporary unavaliable"
After
$xml = simplexml_load_string ( $response );
check if
$xml === false
and set the error message accordingly
http://php.net/manual/en/function.simplexml-load-string.php
As you can read in manual the simplexml_load_string function:
Returns an object of class SimpleXMLElement with properties containing the data held within the xml document, or FALSE on failure.
So simply check if it fails and if it does echo your This service is temporary unavaliable
To get rid of this warning consider to ini_set('display_errors', '0'); on production
I currentley trying to get data from a website, and use it in my PHP script.
The link I am trying to reach is:
http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=★%20Bayonet
Here is my code:
<?php
$skin = $_GET['market_hash_name'];
$link = "http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);
echo $FN;
?>
And this is how I use my link:
http://myhost.com/getPrice.php?market_hash_name=★ Bayonet
This is the error I am getting:
Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=★ Bayonet): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in F:\xampp\htdocs\getPrice.php on line 5
EDIT:
Okay, so I have found a solution for my problem, but now a new error is comming up:
Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=%E2%98%85+Bayonet+%7C+Stained (Factory New)): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in F:\xampp\htdocs\getPrice.php on line 7
This is how I am using my link now:
getPrice.php?market_hash_name=★ Bayonet | Stained
And this is my code:
function getPrice($string) {
$skin = urlencode($_GET['market_hash_name']);
$link = "http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=".$skin." ".$string;
$content = file_get_contents($link);
$data = json_decode($content, true);
return $data['lowest_price'];
}
$FN = getPrice("(Factory New)");
$MW = getPrice("(Minimal Wear)");
$FT = getPrice("(Field-Tested)");
$WW = getPrice("(Well-Worn)");
$BS = getPrice("(Battle-Scarred)");
echo "Factory New: $FN; Minimal Wear: $MW; Field-Tested: $FT; Well-Worn: $WW; Battle-Scared: $BS";
Among other characters, non-ASCII characters must be URL-encoded (aka percent-encoded) in query strings.
$skin = url_encode($_GET['market_hash_name']);
$link = "http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);
I'm trying to get latitude and longtitude from google map by giving address. But it's giving error. When I directly copy url to browser url bar. it's giving correct result. If anybody know the answer.
Here is my code.
<?php
$key = '[AIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0]';
$opt = array (
'address' => urlencode('Kolkata,India, ON') ,
'output' => 'xml'
);
$url = 'http://maps.google.com/maps/geo?q='.$opt['address'].'&output='.$opt['output'].'&oe=utf8&key='.$key;
$dom = new DOMDocument();
$dom->load($url);
$xpath = new DomXPath($dom);
$xpath->registerNamespace('ge', 'http://earth.google.com/kml/2.0');
$statusCode = $xpath->query('//ge:Status/ge:code');
if ($statusCode->item(0)->nodeValue == '200') {
$pointStr = $xpath->query('//ge:coordinates');
$point = explode(",", $pointStr->item(0)->nodeValue);
$lat = $point[1];
$lon = $point[0];
echo '<pre>';
echo 'Lat: '.$lat.', Lon: '.$lon;
echo '</pre>';
}
?>
Following error has occured:
Warning: DOMDocument::load(http://maps.google.com/maps/geo? q=Kolkata%252CIndia%252C%2BON&output=xml&oe=utf8&key=%5BAIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0%5D) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\xampp\htdocs\draw\draw.php on line 19
Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://maps.google.com/maps/geo?q=Kolkata%252CIndia%252C%2BON&output=xml&oe=utf8&key=%5BAIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0%5D" in C:\xampp\htdocs\draw\draw.php on line 19
Notice: Trying to get property of non-object in C:\xampp\htdocs\draw\draw.php on line 26
DOMDocument::load() requires allow_url_fopen to be true.
Check your php.ini-settings and switch it to 1.
See also:
http://php.net/filesystem.configuration.php#ini.allow-url-fopen
What would cause DOMDocument.load to fail loading XML from a URL that is accessible?