file_get_contents在codeigniter中给出错误

时间:2017-06-05 11:26:57

标签: angularjs codeigniter

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

//它包含filename = abcd

 $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 static分配给$ siteaddressAPI,那么它会给出结果。

如何在codeigniter中解决?

1 个答案:

答案 0 :(得分:0)

选项1: 更改php.ini

allow_url_fopen = On

选项2:使用cURL 您可能希望尝试使用curl来检索数据而不是file_get_contents。 curl更好地支持错误处理:

 $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
)