file_get_contents 设置header头参数

PHP--file_get_contents 设置header头参数


//封装function
function to_request($url,$params,$header = 'Content-Type:application/x-www-form-urlencoded'){
    $options['http'] = array(
        'timeout'=>60,
        'method' => "POST",
        'header' => $header,
        'content' => $params
    );
    $context = stream_context_create($options);
    ini_set('allow_url_fopen','on');
    ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)');
    $result = json_decode(file_get_contents($url, false, $context),320);
    return $result;
}
//进行调用
$header = ["Content-type:application/json;chartset=UTF-8", "key:$key" , "Authorization:$sign" , "appId:$appid"] ;
        
$result = to_request($url,$params,$header);


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修复 `file_get_contents(): Invalid php:// URL specified` 错误,你需要检查一下你传递给 `file_get_contents()` 函数的文件路径参数是否正确。 如果你使用了 `php://`,那么你需要确定你要访问的是哪个 I/O 资源,并正确指定相应的流封装协议名称。例如,如果你要访问标准输入,那么你应该使用 `php://stdin`,如果要访问标准输出,那么你应该使用 `php://stdout`,如果要访问标准错误输出,那么你应该使用 `php://stderr`。 如果你要访问一个普通的文件,那么你需要检查一下你传递给 `file_get_contents()` 函数的文件路径是否正确,它应该是一个本地文件路径,如 `/path/to/file`。 另外,你还需要确保你有足够的权限来访问该文件。如果该文件是一个受保护的文件,你可能需要使用 `stream_context_create()` 函数来创建一个上下文资源,并在其中设置相应的认证信息。例如: ```php $url = "https://example.com/protected/file.txt"; $context = stream_context_create([ 'http' => [ 'header' => "Authorization: Basic " . base64_encode("username:password"), ], ]); $file_contents = file_get_contents($url, false, $context); ``` 上面的代码使用 `stream_context_create()` 函数创建一个上下文资源,并在其中设置了 HTTP 认证信息。然后,它使用 `file_get_contents()` 函数来读取 `$url` 对应的文件内容,并将结果保存到 `$file_contents` 变量中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值