file_get_contents无法打开流:HTTP请求失败! HTTP / 1.1 400错误请求

时间:2015-04-14 03:05:06

标签: php json restful-url

我已经提供了RESTful网址,可以加载到我目前正在更新的wordpress网站上。这些网页已经包含RESTful网址,我只是使用较新的网址进行更新。但是,一旦我用新的切换旧的,我得到以下错误:

  

警告:   的file_get_contents(http://rest.vivapvd.com/agenda/52/2015 - 11-02 /)   [function.file-get-contents]:无法打开流:HTTP请求   失败! HTTP / 1.1 400错误请求   /nas/...../includes/thomas_functions.php   第86行

这是第86行

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


  

注意:这可以使用模板页面,该页面由前一位开发人员准备。这是以下页面:

<?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();
?>

3 个答案:

答案 0 :(得分:1)

尝试了很多东西后,出现此错误的原因是客户提供给我的文档,其中包含不可读连字符的url路径。当我复制并粘贴网址&#34; http://rest.vivapvd.com/agenda/52/11 - 02-2015 /&#34;在单词中,它出现在&#34; http://rest.vivapvd.com/agenda/52/11%E2%80%9002%E2%80%902015/&#34;

所以,我随机进入并手动输入每个网址的连字符,结果工作得很好!

感谢您的帮助!这是一个奇怪的。

答案 1 :(得分:0)

似乎无法在该地址找到有效文件。您应该尝试指定文件名...例如:http://rest.vivapvd.com/agenda/52/2015/post.php

此外,您确定在网络服务器上完全设置了REST网址吗?你有没有在mod_rewrite中设置它们(对于apache)?

答案 2 :(得分:0)

状态码400错误请求表示服务器没有问题,但您对请求做错了。 (例如,您使用的是get所需的帖子,或者您没有指定强制参数)

从该URL的代码我假设它要求您发送一个帖子请求。 File_get_contents将发出一个get请求,这可能是一个错误的请求&#34;从服务器的角度来看。

我的建议是不使用file_get_contents但至少使用php的curl模块(或者有几个高级http客户端可用于PHP)。在那里你可以指定http方法等等(例如附加标题,身份验证......)

相关问题