Hello!

I am newbie-newbie in php and I really don't understand why my code is not working. Would appreciate any help 🙂

<?php
$test = file_get_contents('http://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=xml&exintro=1&titles=PHP');
echo strlen($test);
echo $test;
?>

Strlen is 0. I think, that the URL is not working. However if I use some other URL, like http://en.wikipedia.org, it is alright.

Also, if I download the url, it is working:

<?php
$test = file_get_contents('C:\xampp\htdocs\university\gui\plain\api.php.xml');
echo strlen($test);
echo $test;
?>

Strlen is higher than 0 and code is returned.

What's wrong with the first code?
Thank you in advance!

    Do you have error reporting turned on (check the error_reporting and display_errors settings in php.ini; for development the former should be as high as possible (E_ALL), and the latter should be on. If you can't change php.ini, you can use [man]error_reporting[/man] and [man]ini_set[/man] at the start of your script instead.)

    You're getting a 403 Forbidden response back. From the MediaWiki API documentation:

    On Wikimedia wikis, if you don't supply a User-Agent header, or you supply an empty or generic one, your request will fail with an HTTP 403 error. See our User-Agent policy. Other MediaWiki installations may have similar policies.

    You'll need to expand your call to [man]file_get_contents[/man] call to include a stream context:

    $context = stream_context_create(array('http' => array('method'=>'GET', 'header' => "User-agent: Giveyourscriptanamehere\r\n")));
    file_get_contents('http://...', false, $context);
    

    For "Giveyourscriptanamehere", you would of course need to give a more sensible User Agent string; see the MediaWiki page linked to for details.

      Thank you very much, it is working.

      Would you mind to help me with error reporting also? I thought, that I have it turned on, but error 403 didn't appear. Errors like "Fatal error: Call to undefined function" do appear. Tried E_ALL in this part of php.ini:

      ; display_errors
      ;   Default Value: E_ALL
      ;   Development Value: E_ALL
      ;   Production Value: E_ALL
      
      ; display_startup_errors
      ;   Default Value: E_ALL
      ;   Development Value: E_ALL
      ;   Production Value: E_ALL
      
      ; error_reporting
      ;   Default Value: E_ALL
      ;   Development Value: E_ALL
      ;   Production Value: E_ALL
      
      ; html_errors
      ;   Default Value: E_ALL
      ;   Development Value: E_ALL
      ;   Production value: E_ALL
      
      ; log_errors
      ;   Default Value: E_ALL
      ;   Development Value: E_ALL
      ;   Production Value: E_ALL
        demongun wrote:

        "Fatal error: Call to undefined function"

        Perhaps you should concentrate on fixing those?

        Tried E_ALL in this part of php.ini:

        Those bits are documentation. The actual settings are further down. Read the paragraph immediately before the bit you posted:

        ; The following are all the settings which are different in either the production
        ; or development versions of the INIs with respect to PHP's default behavior.
        ; Please see the [i]actual settings later in the document[/i] for more details as to why
        ; we recommend these changes in PHP's behavior.
        [emphasis mine]

          Oh, it was really stupid of me. Yet I am still not seeing the error. My current php.ini (Error handling and logging section, without lines beginning with ";") is:

          error_reporting = E_ALL
          display_errors = On
          display_startup_errors = On
          log_errors = On
          log_errors_max_len = 1024
          ignore_repeated_errors = Off
          ignore_repeated_source = Off
          report_memleaks = On
          track_errors = On
          html_errors = On

          I've also checked php_error_log, but there is nothing about this error.

            You said you were getting error messages about undefined functions. So error messages are being displayed. And you'll need to fix those errors.

              Yes, but I prepared undefined function only to check if errors are shown and most of them are. I am not seeing the error that you have mentioned (403 Forbidden response back), even without User-agent code.

                Well, I don't know why you're not seeing it, then, but

                1. I run your code I get:

                  PHP Warning:  file_get_contents(http://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=xml&exintro=1&titles=PHP): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in - on line 2
                2. When I run it with the changes I proposed in my first reply I get

                  1663<?xml version="1.0"?><api><query><pages><page pageid="24131" ns="0" title="PHP"><ext......s></query></api>
                  Write a Reply...