www.webdeveloper.com
Results 1 to 13 of 13

Thread: PHP URL call

  1. #1
    Join Date
    Oct 2012
    Posts
    31

    PHP URL call

    Hi. I am a PHP starter and need your help. I have a form that searches a database for a report based on subject's ID number or title. The report resides on a link that has the following form:

    example.com/thesis_titles.jpf?studyno=43758495&passwd=HBD764UK&IDno=something&title=

    How to call that URL in php, so that I get the report for any ID number or title I search? Either the ID number or the title will be empty in the URL based on what the user searches. The user ID and password is the same for all users.

    I am not sure if I explained myself well, but I hope I could get some help from here.

    Thanks in advance
    Atrisa
    Last edited by Atrisa Milani; 01-09-2013 at 03:28 AM.

  2. #2
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    plenty of possibilities:
    https://www.google.nl/search?q=php+contents+url

    I find it very insecure though that passwords can be simply put in the url

  3. #3
    Join Date
    Oct 2007
    Posts
    371
    I agree with the warning about the password in th URL. If you contol that domain, consider changing that approach.

    You can use file_get_contents for a very simple data grab, or you can read up on cURL for a more complex interaction with the requested page.

    Is this an API of some kind, or are you scraping from an HTML page?

  4. #4
    Join Date
    Oct 2012
    Posts
    31
    Thank you both for your answers. I hid the username and password data from the URL.
    The report comes from an API and not from HTML page. In my case I guess the file_get_contents function will work fine, though I am still not there

  5. #5
    Join Date
    Oct 2012
    Posts
    31
    When searching with a specific ID number, The URI for the report with that ID gets printed in the window and not the report. This line is responsible for getting the report:

    echo getReport(getURI(($rID)));

    and then getReport function:

    function getReport ($uri) {

    $rep = file_get_contents($uri);

    return $rep;

    }

    &uri is the URI in the form I mentioned in the first post and where the report resides. With these I get the report URI in to the frame, but not the report for that specific URI.

  6. #6
    Join Date
    Oct 2012
    Posts
    31
    I though the problem was because of the iFrames I was using, but that is not it.
    Last edited by Atrisa Milani; 01-10-2013 at 10:00 AM.

  7. #7
    Join Date
    Oct 2012
    Posts
    31
    Found the reason. Problem was not in the code, rather in our environment.

  8. #8
    Join Date
    Sep 2008
    Posts
    95
    Maybe I'm mis-interpreting the original question, but in case anyone else is reading this thread and interprets it the way I did...

    If you have a url like the one given:
    http://mysite.com/page.php?id=123&myvar=test

    You can access those variables in PHP quite easily using $_GET.

    So, for example, if that url is used on a page to run the search, we could use code such as the following...

    Code:
    <?php
    $id=isset($_GET['id'])?(int)htmlentities($_GET['id'],ENT_QUOTES,'utf-8'):'';
    $myvar=isset($_GET['myvar'])?(string)htmlentities($_GET['myvar'],ENT_QUOTES,'utf-8'):'';
    
    mysql_query("SELECT * FROM mytable WHERE id=$id OR myvar='$myvar'");
    ?>

    BE SURE TO CLEAN/VALIDATE YOUR VARIABLES BEFORE USING THEM IN A SQL QUERY TO AVOID INJECTION.

  9. #9
    Join Date
    Oct 2012
    Posts
    31
    Thanks speghettiCode. I now get the report out.

    One question yet: the report is using Scandivian characters such as and . The report looks fine on the URL in the browser, but when I get the same report with my PHP code, those special characters are not recognized and the report looks funny. Is there a way I could define the character set in my code so that the report shows correctly?

    Also the styles and pictures in the report are missing.

    Thanks
    Last edited by Atrisa Milani; 01-18-2013 at 08:55 AM.

  10. #10
    Join Date
    Sep 2008
    Posts
    95
    Those characters are part of a UTF-8 character set. To set the default character set via PHP, include the following line at the top of your page:

    PHP Code:
    ini_set('default_charset','UTF-8'); 
    Also, it may be redundant, but I also include the following HTML at the top of my pages:

    HTML Code:
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
    If your database query is not pulling the information you would expect, there are several factors:
    1. table structure
    2. how the variables are encoded in the table
    3. how the variables are encoded in the query

    If you are using the code I suggested, then will be changed to &Aring; when stored in the variable (or, perhaps displayed in the report depending on how you have it set up). To reduce the risk of SQL injection it's important to use htmlentities() or something similar when using the variables in a query. However, if you need these characters displayed on the screen, you could use:

    PHP Code:
    echo html_entity_decode($myvar,ENT_QUOTES,'utf-8'); 
    Does that help?
    Last edited by speghettiCode; 01-18-2013 at 11:01 AM. Reason: added HTML meta tag

  11. #11
    Join Date
    Oct 2012
    Posts
    31
    Thanks for the reply. I have that meta tag in the first page; also tried ini_set('default_charset','UTF-8'); but still the same problem. My code looks like this:
    Code:
    function getData ($url) {
    
        $data = file_get_contents($url);
        $data = utf8_decode($data);
        return $data;
    }
    But this didn't help either.

  12. #12
    Join Date
    Oct 2012
    Posts
    31
    This did the trick: $data = utf8_encode($data); Looked like the data was in ISO-8859-1 and had to convert it to UTF-8. Thanks for your help so far.

  13. #13
    PHP is server side scripting language. It is use for designing web page. PHP may be deployed on most internet servers and additionally as a standalone shell on nearly each operating system.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles