首页 > 解决方案 > 使用 file_get_contents 发布 json

问题描述

在尝试创建脚本之前,我首先尝试使用邮递员应用程序,一切顺利,没有任何限制,但是在我使用我制作的脚本后,结果与应用程序邮递员给出的不一样。

这是邮递员应用程序的结果 这是邮递员应用程序的结果

这是从邮递员那里获得的输出结果 这是从邮递员那里获得的输出结果

然后,我尝试使用我创建的脚本,如下所示

<?php
$postdata = http_build_query(
array (
  'question' => 'this is questions',
  'questionMedia' => '',
  'choices' => 
  array (
    0 => 
    array (
      'text' => '123123123',
    ),
    1 => 
    array (
      'text' => 'qweqweqwe',
    ),
    2 => 
    array (
      'text' => 'asdasdasd',
    ),
    3 => 
    array (
      'text' => 'zxczxczxcxc',
    ),
  ),
  'allowMultiChoice' => false,
  'endDate' => NULL,
  'enableCaptcha' => false,
  'hideResults' => false,
  'restriction' => 'ip',
)
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'content' => $postdata,
        'header'  => 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36\r\n'
    )   
);

$context  = stream_context_create($opts);
$result = file_get_contents('https://www.polltab.com/api/poll/create', false, $context);
echo $result;
?>

但是,从上面的脚本中,会产生如下所示的输出。

这是从脚本获得的输出结果 这是从脚本获得的输出结果

标签: phppostmanfile-get-contents

解决方案


推荐阅读