dpo15099 2015-10-23 17:04
浏览 77

为什么我的HTTP请求从PHP失败?

I try to submit a form to remote server: http://openconvert.clarin.inl.nl/openconvert/tagger#text, as you can see the required fileds are input and format, and the action url is http://openconvert.clarin.inl.nl/openconvert/text/, so I try the following in php:

<?php
function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
    'method' => 'POST',
    'content' => $data
  ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}
$remoteurl="http://openconvert.clarin.inl.nl/openconvert/text";
$rawdata=array(
  'input'=>'test',
  'format'=>'text');
$data=http_build_query($rawdata, '', '&amp;'); //flags_, myvar_
var_dump($data);
echo "<br />";
echo do_post_request($remoteurl,$data,'Content-Type: text/html');
?>

I don't know why the server can't find my data?

The output is:

string(26) "input=test&amp;format=text"
<br /><?xml version="1.0" encoding="UTF-8"?>
<error>Insufficient parameters. Required parameters: input, format</error>
  • 写回答

1条回答 默认 最新

  • dsxpt62448 2015-10-23 17:17
    关注

    There might be other problems, but the &amp; value in http_build_query is definitely wrong. You're not embedding your urlencoded data into an html document, so you should not html-escape the ampersand. Replacing that line with:

    http_build_query($rawdata, '', '&amp;'); 
    

    should be better, but it might not be 100% of your solution.

    P.S.: Don't use @, ever. You are suppressing real errors that you should see.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构