file_get_content(converting to htmlspecialchars) - php

So I have this url http://my-server.com/feeds/feed.pl?op=retrieveIncidents&type=social,government&cbsa=31100 that contains xml data built by another process. Now I was using file_get_contents() with the url hardcoded and it worked perfectly. At this stage of dev I had to start using the config files and the cbsa codes from the current user logged in the application.
Now this /feeds/feed.pl?op=retrieveIncidents&type=social,government&cbsa= comes from the config.
This 31100 comes from the user object.
This http://my-server.com comes from a helper fn().
$server = $this->serverUrl();
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);
$curOperator = Default_Model_Operator::getActiveOperator();
$feedUrl = $server.$config->ims->idfFeed.$curOperator->regions;
If I print/echo $feedUrllooks like http://my-server.com/feeds/feed.pl?op=retrieveIncidents&type=social,government&cbsa=31100
But when I do $feed = file_get_contents($feedUrl) I get
Warning: file_get_contents(http://vdot.orion.meridian-enviro.com/feeds/feed.pl?op=retrieveIncidents&type=social,government&cbsa=31100) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in /home/Agena/lvegerano/svn/webapps/ims/trunk/phpfiles/application/views/scripts/chp/list-for-dashboard.phtml on line 9
Make note of the &. I have tried htmlentities_decode(),urlencode()and other and cant get this to works. What can I do so file_get_contents() translate &to&?
Any help on how to fix this would be greatly appreciated.

I guess I forgot to answer this. The problem was our varnish setup. It is setup to lookup urls and if recognized inserts the host name. Because I had just started working here I was not aware of this feature. Therefore the error I thought it was it wasn't since the varnish was masking the url.

Related

file_get_contents() "failed to open stream: Network is unreachable" error on valid link

There's an extremely simple example of using Youtube API in php that somehow fails to work in my case, and I couldn't find a definitive solution for.
I desire to use the Youtube API without any wrapper, by myself, to get data of videos. The following search:list query works perfectly when I try accessing it from within by browser (as a link), but in php, I get that error when I try the same.
$apikey_YT = <my API key>;
$ytrequrl = "https://www.googleapis.com/youtube/v3/search?".
"part=snippet".
"&maxResults=50".
"&q=doom".
"&relatedToVideoId=h8j2zj-A5tE".
"&type=video".
"&key=${apikey_YT}";
$result = file_get_contents($ytrequrl);
var_dump($result);
The potential issues were URL encoding, or allowing allow-url-fopen, but neither seemed to help in my case: the former actually gave a new error message: No such file or directory.
What can I do?
Try it like this. Might be your formatting.
This works for me.
$ytrequrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=doom&relatedToVideoId=h8j2zj-A5tE&type=video&key='.$apikey_YT;
$info= file_get_contents($ytrequrl);
$info= json_decode($info, true);
print("<pre>".print_r($info,true)."</pre>");

where does this URL need urlencoding?

I hosted a website i worked on on amazon web services, and for some reason some things dont work compared to when I run it locally on localhost.
of those things are the
if (array_key_exists("error", $json))
function and
the file_get_contents function.
Ive commented out the array key exists part and that solved the issue, at least for that part of logging in, until i get to the view documents page where a slim application error displays
failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
and the issue for that lies in the viewdocspage.php file, particularly this code:
<?php
$raw = file_get_contents("http://cosoft.us-east-1.elasticbeanstalk.com/cosoft/mywiki/api.php?action=query&list=allpages&format=json");
$pages_response = json_decode($raw, true);
$pages_array = $pages_response["query"]["allpages"];
$page_titles = [];
I looked and read up on many threads that the issue lies with the file_get_contents in which the URL contains special characters, such as spaces (which mine doesnt have) and so needs to be encoded, using urlencode (or rawurlencode..?)
now ive tried encoding the whole url like this:
$raw = file_get_contents(urlencode("http://cosoft.us-east-1.elasticbeanstalk.com/cosoft/mywiki/api.php?action=query&list=allpages&format=json"));
but that resulted in this error:
slim error
Message: file_get_contents(http%3A%2F%2Fcosoft.us-east-1.elasticbeanstalk.com%2Fcosoft%2Fmywiki%2Fapi.php%3Faction%3Dquery%26list%3Dallpages%26format%3Djson): failed to open stream: No such file or directory
I figured that this may happen since I read that not all the URL should be wrapped by this encoding, but heres where Im stuck: which part of the url do i use the encoding on? the only special characters i keep coming accross regarding this error is spaces, but i dont have any spaces, so its something else which i dont know what it is...
Help is appreciated, thanks!
You would just need to url_encode the parameters. Say you had a value $value='My name is earl'
If you wanted to pass this value as a parameter in your url
http://somesite.com/?name=$value there would be spaces in the value that is url_encoded. So if you encode it as 'http://somesite.com/?name='.urlencode($value), when this is encoded the value will turn into My+name+is+earl
Reading resources from a URL may be restricted by the server's configuration.
http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
You should use CURL for this.

php scripts that suddently stops working - where to start looking?

If someone doesn't feel like reading the whole explanation, you can skip to the last 2 paragraphs where I have the actual question
So I have this bunch of websites made with CakePHP. In one of the pages (this is the same for all the websites), I load an XML file and process its contents (using the XML helper). This has been working for quite some time. Now, suddenly, it doesn't work anymore in any of the websites (and the code hasn't changed a bit), it gives an "error opening file".
At the same time, another script of those websites (which has nothing to do with the previous one, it's plain PHP, outside the cakephp "area") has stopped working too. In this case, it's a scripts that creates a connection with a video hosting provider (Fliqz) and retrieves the URL of a video given a video id. Suddently, it throws an error something like "session was not created" (the script creates some sort of session with Fliqz' server to get the URL).
All this sounds very weird, I know. The first thing I've done, after checking that the code is correct, is call 1&1 (the hosting provider) and ask if they have made any change in the server. They said that they changed to PHP5 a few months ago (which I was aware of, and everything was working fine after that change), but that they haven't changed anything else.
My question would be, is there anything I can do to figure out what is causing these problems? For example, I found some post of someone saying that his hosting had restricted some functionality that allowed external connections. Or in another post I saw a suggestion about doing this: echo file_get_contents("http://google.com/") and I got an error, meaning that some setting wasn't right. I even tried to use simplexml_load_file (instead of the XMl helper) in the page where I load the XML file and didn't work, giving another error...
I would like to know, if a PHP setting is causing this, which one could it be, so I can look at it and call again 1&1 to tell them what they need to change (if there's anything to change). Otherwise, how in the world can something just stop working?
Thank you for any advice! It is much appreciated!
EDIT: detailed explanation of errors
1. Error related to the XML not loading using CakePHP's XML helper
This is the code I use (which used to worked perfectly):
$completeurl = '/full/path/to/file/myfile.xml';
App::import('Xml');
$xml =& new XML($completeurl);
$this->list = Set::reverse($xml);
I don't get any error or warning. If I debug $xml, I get this:
Xml Object
(
[__parser] =>
[__file] =>
[__rawData] =>
[__header] =>
[__tags] => Array
(
)
[version] => 1.0
[encoding] => UTF-8
[name] => #document
[namespace] =>
[namespaces] => Array
(
)
[value] =>
[attributes] => Array
(
)
[children] => Array
(
)
[__parent] =>
)
If I debug $this->list I get an empty array. Before this problem, I would get an associative array with all the information of the XML file, perfectly structured. Oh, and the XML file is the same it used to be, and I've revalidated it just in case giving no errors.
2. Error related to XML file not loading using simplexml_load_file()
This is the code I've tried with "regular" php (without using any CakePHP helper):
$file = '/full/path/to/file/myfile.xml';
if(!$xml = simplexml_load_file($file))
exit('Failed to open '.$file);
And this are the errors (warnings):
Warning (2): simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration [APP/plugins/icd_discovery/webroot/videos.php, line 29]
Warning (2): simplexml_load_file(http://discovery.ingles100.com/icd_discovery/xml/videos_unidades.xml) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found [APP/plugins/icd_discovery/webroot/videos.php, line 29]
Warning (2): simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://discovery.ingles100.com/icd_discovery/xml/videos_unidades.xml" [APP/plugins/icd_discovery/webroot/videos.php, line 29]
If I put a relative path for the file, I just get the last warning, but still not loading it.
** 3. Error related to script to load URL of hosted videos in Fliqz**
The page that contains this script is totally unrelated to the previous ones (meaning that the code there doesn't affect the code here). We use the following script (provided by Fliqz) to request the URL of our videos hosted there:
include_once '../fliqz/dlMetrics.php';
// initializing new session
$videoID = '123123123';
$applicationID = '456456456';
$uniqueID = null;
// create a new fliqzDownload Object
$metricsObj = new dlMetrics();
// get new session, using Application ID from Account:
$sessionID = $metricsObj->newSession($applicationID,NULL,NULL,NULL,$uniqueID);
// show server time when new session was created:
$start = $metricsObj->getSessionStart();
// Get asset location (note: must use dashes in asset ID example: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
$video_URL = $metricsObj->initLoad($videoID);
By doing this, we would get the URL of the video in the $video_URL variable. Again, this used to work perfectly. Suddenly, it doesn't.
So is it a coincidence that all this things stop working? I think that something has changed somewhere. Not the code, I'm the only one who touches that. The PHP version changed a while ago, and everything was working fine. Something in the server? 1&1 says that they didn't, or at least the person I talked to. If I have to call back, I would like to be able to point a little better where do I think the problem might be coming from, that's why I need you help guys!
Again, thanks for any advice!
Create a file called php.ini with the following content:
allow_url_fopen = ON
and place it in the same folder (or folders) as those scripts.

PHP - file get contents not working when passing URL from database otherwise works fine

I am trying to get contents of a web page using file_get_contents
Scenario One:
When I pass a single URL directly from a text field it works perfect.
Scenario Two:
When I take multiple URLs in a text area and then store them in a Mysql database. Then I run a for loop to pass the URL to the same code snippet as in scenario 1 it returns this error
Warning: file_get_contents( URL ) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in C:\wamp\www\scrape\urltoscrape.php on line 13
I felt that the prob was with the datatype being used. So it was initially varchar I changed it to text.
I also tried strip space. When I echo the URL out it prints it as is with no additional characters or spaces.
Also since it is in a loop I do not think the issue is with multiple URLs being passed at a time. All the URLs individually work fine when tried through scenario 1.
Please help. I am at my wits end.
file_get_contents( URL )
This error tells you, it is trying to open the URL "URL". Make sure you did not forget a $ before the variable name.
file_get_contents() expects a string which it has to follow. Now if you write url as its argument, it will try to fetch the contents of URL
try
file_get_contents($url);

Magento API call for getting products information

I used the below code to get products details from a magento store from my localhost
$proxy = new SoapClient('http://domain.com/magento/index.php/api/soap/?wsdl');
$sessionId = $proxy->login('username', 'apikey');
$filters = array(
'sku' => array('like'=>'test%')
);
$products = $proxy->call($sessionId, 'product.list', array($filters));
var_dump($products);
It works on my localhost machine but not on server. But soap configuration is enabled in server. Below is the error message
" SOAP-ERROR: Parsing WSDL: Couldn't find <definitions> in "
I got corresponding xml file when I used the below URL
http://domainname/shoponline/index.php/api/soap/?wsdl
I removed index.php but didn't get any result.
$proxy = new SoapClient('http://domain.com/magento/index.php/api/soap/?wsdl');
The above statement display the below error
Parsing WSDL: Couldn't find definitions in
Looks like the WSDL XML file was not loaded at all. To debug, try to open this file in your browser.
I guess it will not be loaded at all and you'll get an error (which will help you to find out what's wrong) or you will be asked to input username and password (basic HTTP auth). In the second case try change your URL from http://domain.com/magento/index.php/api/soap/?wsdl to http://user:pass#domain.com/magento/index.php/api/soap/?wsdl
Make sure you disable developer mode and error reporting in index.php.
Open the "hosts" file from C:\windows\system32\drivers\etc and comment out the line ::1 (like this: # ::1) or remove the line altogether, then save the file.
I've also used this service - http://localhost:81/magento/index.php/api/soap/?wsdl and it works for me.
The SoapClient unable to retrive wsdl definition file which means that routing is not configured correctly or there some external error occurs. But most likely it's a routing problem. The api/[type] rewrite rule is commented in .htaccess, so you need to remove # sign or use api.php directly. To make api.php work directly you need to pass type parameter to api.php to specify which API protocol you would like to use.
For example, for SOAP it will be - http://domain.com/magento/api.php?type=soap&wsdl. You can also use 'soap_v2'.

Categories