简体   繁体   中英

file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in

I've been provided RESTful URLs to load onto a wordpress site I'm currently updating. These pages already have RESTful URLs, I'm just updating them with newer URLs. However, once I switch the old with the new I get the following error:

Warning: file_get_contents( http://rest.vivapvd.com/agenda/52/2015 ‐11‐02/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /nas/...../includes/thomas_functions.php on line 86

Here is line 86

function fetch_url_content($url=false){
    if(empty($url)) return $url;
    $contents   = file_get_contents($url);
    $array      = json_decode($contents,true);
    return $array;
}


NOTE: This is suppose to work with a template page, which was prepared by the previous developer. Here's the page below:

<?php
/*
    Template Name: Agenda Pages
*/
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_sidebar_content' );
remove_action( 'genesis_after_content', 'genesis_get_sidebar' );
add_action( 'genesis_after_content', 'td_custom_page_sidebar' );
/*---------------------------------------------------------------------------------------*/
add_action('genesis_after_loop', 'agenda_restful_content');
function agenda_restful_content(){
    global $post;
    $url = get_post_meta($post->ID, 'wpcf-rest-url', true);
    $agenda = fetch_url_content($url);
    if($agenda){
        //echo '<pre>'; var_dump($agenda); echo '</pre>';
        $topictitle=false;
        if(isset($agenda[0]['TopicTitle']) && !empty($agenda[0]['TopicTitle']))
            echo '<h2>'.$agenda[0]['TopicTitle'].'</h2>';
        echo '<table>';
            echo '<tbody>';
        foreach($agenda as $a){
            if($topictitle===false)
                $topictitle=$a['TopicTitle'];

            if( $topictitle!=$a['TopicTitle'] ){
                $topictitle=$a['TopicTitle'];               
                echo '</tbody></table>';
                echo !empty($a['TopicTitle']) ? "<h2>$a[TopicTitle]</h2>" : '';
                echo '<table>';
                    echo '<tbody>';
            }
        ?>
            <tr>
                <th class="time"><?php echo date('g:i A', strtotime($a['StartDate'])) ?>-<?php echo date('g:i A', strtotime($a['EndDate'])) ?></th>
                <td>
                    <?php echo isset($a['Title'])           ? $a['Title'].'<br>'    : ''; ?> 
                    <?php echo isset($a['SpeakerNames'])    ? $a['SpeakerNames']    : ''; ?>
                </td>
            </tr>
        <?php 
        }
        echo '</tbody></table>';
    }
}
/*---------------------------------------------------------------------------------------*/
genesis();
?>

After trying many things, the reason for this error was in the document provided to me by the customer, which contained url paths with no-readable hyphens. When I copy and pasted the url " http://rest.vivapvd.com/agenda/52/11 ‐02‐2015/" into word, it came up as " http://rest.vivapvd.com/agenda/52/11%E2%80%9002%E2%80%902015/ "

So, I randomly went in and manually typed in the hyphens per url and it ended up working just fine!

Thanks for all your help guys! This was a strange one.

It seems like it can't find a valid file at that address. You should try specifying the file name... such as: http://rest.vivapvd.com/agenda/52/2015/post.php

Also, are you sure REST urls are fully set up on the web server? Did you set them in mod_rewrite (for apache)?

The status code 400 bad request indicates that there is no problem with the server but you are doing something wrong with the request. (Eg you are using get where post would be required or you did not specify mandatory parameters)

From the code of that URL I assume that it requires you to send a post request. File_get_contents will make a get request, which is arguably a "bad request" from the servers standpoint.

My recommendation would be to not use file_get_contents but at least php's curl module (alternatively there are several high level http clients available for PHP). There you can specify the http method and much more (eg additional headers, authentication,...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM