的file_get_contents给出了错误的笨

问题描述:

$scope.formatfilename = response.fileformat[0].FileName; 

//它包含文件名= ABCD的file_get_contents给出了错误的笨

$filename1 ='{{formatfilename}}'; // now I have taken into phpvariable 
$siteaddr = "http://localhost:8080/demoproject/document/".$filename1.".html"; 
echo file_get_contents($siteaddressAPI); 

它给了我未能打开流:HTTP请求失败! HTTP/1.0 400错误请求 如果我将完整的URL静态分配给$ siteaddressAPI,那么它会给出结果。

如何解决codeigniter?

+0

尝试用urlencode - '$ siteaddr =用urlencode($ siteaddr);' –

+0

您可以定义并设置'$ siteaddr',然后尝试在'$ siteaddressAPI'处读取文件。 '$ siteaddressAPI'从哪里来的? – DFriend

+0

我已经使用file_get_contents()方法读取了该文件。问题是,当我将变量名传递给$ siteaddr时,它会给出错误的请求,但是当我直接将url传递给file_get_contents()方法时,它显示输出 –

选项1:使用卷曲 您可能需要使用curl来检索数据,而不是的file_get_contents尝试:在php.ini

allow_url_fopen = On 

选项2 变化。卷曲有错误处理的更好的支持:

$filename1 ='{{formatfilename}}'; // now I have taken into phpvariable 
$siteaddr = "http://localhost:8080/demoproject/document/".$filename1.".html"; 
// make request 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $siteaddr); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 

    // convert response 
    $output = json_decode($output); 

    // handle error; error output 
    if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) { 

     var_dump($output); 
    } 

    curl_close($ch); 

工作实例

<?php 

$siteaddr = "https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY"; 
// make request 
$ch = curl_init(); 
// Disable SSL verification 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// Set the url 
curl_setopt($ch, CURLOPT_URL, $siteaddr); 
// Execute 
$output = curl_exec($ch); 

// convert response 
$output = json_decode($output); 

// handle error; error output 
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) { 
    var_dump($output); 
} 

curl_close($ch); 
echo "<pre>", print_r($output), "</pre>"; 
die(); 
?> 

输出

stdClass Object 
(
    [error_message] => The provided API key is invalid. 
    [results] => Array 
     (
     ) 

    [status] => REQUEST_DENIED 
) 
+0

它不会打印任何内容。 –

+0

你尝试过'print_r($ output);''curl_close($ ch)后'''? –

+0

是的,我试过,但它返回为NULL。 –