For 2 days I'm having trouble with my PHP script on my server. I've changed nothing and suddenly it didn't work anymore.

2天来,我的服务器上的PHP脚本有问题。我什么也没变,突然间就不工作了。

Here is the code:

这是代码:

$query = http_build_query($data);
$options = array(
    'http' => array(
        'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
                    "Content-Length: ".strlen($query)."\r\n",     
        'method'  => "POST",
        'content' => $query,
    ),
);
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n",'method'  => 'POST',
        'content' => http_build_query($data),));
$contexts = stream_context_create($opts);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $contexts, -1, 40000);

I'm getting these error messages:

我收到这些错误信息:

Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in

注意:file_get_contents(): Content-type未指定假设应用程序/x-www-form-urlencode。

Warning: file_get_contents(https://mobile.dsbcontrol.de): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in

警告:file_get_contents(https://mobile.dsbcontrol.de):未打开流:HTTP请求失败!HTTP/1.1 500内部服务器错误。

But when I try the script locally it works perfectly.

但是当我在本地尝试脚本时,它的效果非常好。

2 个解决方案

#1


25

You are passing $contexts to file_get_contents() and that only contains the User-Agent header in the $opts array. All other headers and options are in the $options array which you add in to $context but aren't using. Try:

您将$context传递给file_get_contents(),它只包含$opts数组中的User-Agent头。所有其他的标题和选项都在$options数组中,您将它添加到$context中,但是没有使用。试一试:

$query = http_build_query($data);
$options = array(
    'http' => array(
        'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
                    "Content-Length: ".strlen($query)."\r\n".
                    "User-Agent:MyAgent/1.0\r\n",
        'method'  => "POST",
        'content' => $query,
    ),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context, -1, 40000);

更多相关文章

  1. 在php和servlet应用程序之间进行通信
  2. 使用PHP和MySQL开发“测试”Web应用程序的数据库设计
  3. 显示长期运行的PHP脚本的进展。
  4. 数据库是存储我的Web应用程序数据的最佳选择吗?
  5. PHP基础之脚本传参
  6. phpBB3导入帖子的Python脚本
  7. 架构比MVC更适合Web应用程序?
  8. PHP脚本在我的Wordpress文件中。请解码或帮助我删除它。
  9. AJAX调用在JavaScript中运行PHP脚本

随机推荐

  1. Android Studio错误“AppCompat不支持主
  2. 如何使用2个旋转圆圈获得与ICS相同的未定
  3. 有关WebView的一些使用方法
  4. 将不同的Android项目连接到现有的Google-
  5. Linux上杀死eclipse进程
  6. 用vs2015开发Android靠谱吗
  7. Android笔记(9)---Fragment的生命周期以及
  8. Android4.X物理按键监听
  9. Android VR入门文章
  10. Android Handler处理机制 ( 一 )(图+源码