• Hi

    I was using the php include function to get some content from some outside sites with such a script in a page:

    <?php 
    include "https://www.blablabla.com/abc/single.php"; 
    ?>

    I was more less forced to change the web host and eventually discovered this and a number of php functions are blocked on the current server, supposedly for security reasons. Possibly because this host always gets good reviews in Germany and so think everything they are doing is right, it has not been possible to convince them to allow these functions, including “include”, actually allowed on all servers I have had or manage sites. Their only option is expensive VPS, which does not pay for this small site.

    It’s been possible to use JavaScript to get this specific content from the source, but it has some limitations.

    So my question: is there any alternative php function that can do exactly that, to insert content in a page?
    I’m not a coder and would, if there’s a solution want one easy to ue like the above piece of code.

    Thank you …

    • This topic was modified 6 years, 8 months ago by xprt007.
Viewing 4 replies - 1 through 4 (of 4 total)
  • I doubt your host as blocked the include function, it’s too important, they’ve likely just blocked using include on URLs (rather than local paths). There are better ways to do what you’re after though. WordPress offers some functions for retrieving content from external URLs.

    The first one is wp_remote_get(). Use that function to make an HTTP request to the desired URL:

    $request = wp_remote_get( 'https://www.blablabla.com/abc/single.php' );
    

    Then, to get the actual content, use wp_remote_retrieve_body():

    $request = wp_remote_get( 'https://www.blablabla.com/abc/single.php' );
    $response = wp_remote_retrieve_body( $request );
    

    Which you can then output with echo:

    $request = wp_remote_get( 'https://www.blablabla.com/abc/single.php' );
    $response = wp_remote_retrieve_body( $request );
    echo $response;
    

    I’d just suggest adding some conditions to make sure the request is successful before attempting to output the response:

    $request = wp_remote_get( 'https://www.blablabla.com/abc/single.php' );
    
    if ( ! is_wp_error( $request ) ) {
        $response = wp_remote_retrieve_body( $request );
        echo $response;
    }
    
    Thread Starter xprt007

    (@xprt007)

    Hi

    Thank you for the quick response. You are probably right about the include function. In the past, I have had to give up a number of WordPress plugins on the same host server, including one using passthru(), system() and exec() [WP DB manager], etc and as said, discovered I could not get this content using include, although I had it on several Drupal & WordPress sites for years. Anyway, in spite of explaining, it did not help, which was very bad for me, because the specific pages using the include function are the most visited on 2 sites.

    It’s great to know there’s an alternative, but my level of knowledge enabled me put some simple include code together with “https://www.blablabla.com/abc/single.php&#8221; and the scripts provided by the content source coder in “/abc/” got content from the remote source, which was then printed in the WordPress (or Drupal) page.

    So I’m at a loss regarding what to insert into the page/post in place of

    <?php 
    include "https://www.blablabla.com/abc/single.php"; ?>

    .
    Where exactly does the rest of that code go? As said, not a coder.

    I’d very much appreciate some more guidance.

    Thank you in advance.

    • This reply was modified 6 years, 8 months ago by xprt007.
    • This reply was modified 6 years, 8 months ago by xprt007.
    Thread Starter xprt007

    (@xprt007)

    Update:

    I’m checking …
    It seems you had answered my question before in the last portion of your response, but still testing.

    • This reply was modified 6 years, 8 months ago by xprt007.
    Thread Starter xprt007

    (@xprt007)

    $request = wp_remote_get( 'https://www.blablabla.com/abc/single.php' );
    
    if ( ! is_wp_error( $request ) ) {
        $response = wp_remote_retrieve_body( $request );
        echo $response;
    }

    Many, many thanks. That code solved my problem! ??

    Best regards

    • This reply was modified 6 years, 8 months ago by xprt007.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Is there an alternative to the php include function?’ is closed to new replies.