Failed to load url in Dom Document? - php

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?

Related

php soapserver: Can't import external schema

I am implementing a PHP SOAP server using a third party wsdl. The included xsd has an reference to the external schema as bellow:
<xsd:import schemaLocation="http://www.w3.org/TR/2008/REC-xmldsig-core-20080610/xmldsig-core-schema.xsd" namespace="http://www.w3.org/2000/09/xmldsig"/>
The SoapClient with bellow code works perfectly:
$options = array();
$options['features'] = 1;
$options['trace'] = 1;
$options['connection_timeout'] = 2000;
$options['exceptions'] = 1;
$options['soap_version'] = SOAP_1_1;
$options['encoding'] = 'UTF-8';
$options["user_agent"] = 'php-file_get_contents/'.phpversion();
$clent = new SoapClient( $wsdl, $options);
But not the SoapServer
$options = array();
$options['features'] = 1;
$options['trace'] = 1;
$options['connection_timeout'] = 2000;
$options['exceptions'] = 1;
$options['soap_version'] = SOAP_1_1;
$options['encoding'] = 'UTF-8';
$server = new SoapServer($wsdl, $options);
Which trows the following error
SoapServer::SoapServer("http://www.w3.org/TR/2008/REC-xmldsig-core-20080610/xmldsig-core-schema.xsd"): failed to open stream: HTTP request failed! HTTP/1.0 500 Server Error\r\n in ..../index.php on line 54
PHP Warning: SoapServer::SoapServer(): I/O warning : failed to load external entity "http://www.w3.org/TR/2008/REC-xmldsig-core-20080610/xmldsig-core-schema.xsd" in ..../index.php on line 54
PHP Fatal error: SOAP-ERROR: Parsing Schema: can't import schema from 'http://www.w3.org/TR/2008/REC-xmldsig-core-20080610/xmldsig-core-schema.xsd' in .../index.php on line 54
The SoapClient was also throwing the same error, but which is resolved after adding the "user_agent" option.
I don't see a "user_agent" option for SoapServer.
am I missing something here?
I am able to access the url: http://www.w3.org/TR/2008/REC-xmldsig-core-20080610/xmldsig-core-schema.xsd and able to download the xsd file
Edited: Added the actual xsd link.

file_get_contents failed to open stream : invalid argument with URL

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.

Invalid argument supplied for foreach() - can't loop through two arrays

My code should convert "defindex" from array_inv into "item_name" from array_schema:
<?php
$apikey = "X";
$steamid = $steamprofile['steamid'];
$url_inv = "http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=". $apikey . "&steamid=" . $steamid . "&format=json";
$url_schema = "http://git.optf2.com/schema-tracking/plain/Counter%20Strike%20Global%20Offensive%20Schema?h=counterstrikeglobaloffensive";
$array_inv_raw = file_get_contents($url_inv);
$array_schema_raw = file_get_contents($url_schema);
$array_inv = json_decode($array_inv_raw,true);
$array_schema = json_decode($array_schema_raw,true);
foreach($array_inv['result']['items'] as $item){
foreach($array_schema['result']['items'] as $schemaItem){
if($item['defindex'] == $schemaItem['defindex']){
echo $schemaItem['item_name'].'<br />';
break;
}
}
}
?>
But it's resulting in these errors:
Warning:
file_get_contents(http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json):
failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in
/home/kartm/public_html/scripts/inv.php on line 6
Warning: Invalid argument supplied for foreach() in
/home/kartm/public_html/scripts/inv.php on line 12
The urls:
array_inv
array_schema
I can't find any mistakes. Can you tell me what's wrong with this code?
These errors were caused by a typo in a url:
http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json
It should have been & key=x instead of ?key=x.
file_get_contents couldn't fetch the url, thus the foreach had an invalid, empty argument.

How to use file_get_contents() with non-ASCII characters in URL?

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&currency=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&currency=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&currency=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&currency=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&currency=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&currency=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);

file_get_contents works on localhost but times out on server

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

Categories