Php 为什么无法访问POST值?
我正在使用它向我正在开发的Web服务发出HTTP POST请求 我使用服务描述JSON文件进行此操作,如下所示:Php 为什么无法访问POST值?,php,post,http-post,httprequest,guzzle,Php,Post,Http Post,Httprequest,Guzzle,我正在使用它向我正在开发的Web服务发出HTTP POST请求 我使用服务描述JSON文件进行此操作,如下所示: { "name": "My webservice", "apiVersion": "1.0", "description": "My webservice is a webservice", "operations": { "createStuff": { "httpMethod": "POST",
{
"name": "My webservice",
"apiVersion": "1.0",
"description": "My webservice is a webservice",
"operations": {
"createStuff": {
"httpMethod": "POST",
"uri": "/stuff/{accountid}/{product}/things",
"summary": "Creates a thing",
"parameters": {
"accountid": {
"location": "uri",
"description": "The account ID of the stuff"
},
"product": {
"location": "uri",
"description": "The product of the stuff"
}
},
"additionalParameters": {
"location": "body"
}
}
}
}
完成这项工作的代码分为两类(我保证它们不会太长):
StuffApi
class StuffApi
{
protected $client;
public function __construct(ClientInterface $client, $baseUrl)
{
$this->client = $client;
//tell the client what the base URL to use for the request
$this->client->setBaseUrl($baseUrl);
//fill the client with all the routes
$description = ServiceDescription::factory(__DIR__."/../resources/routes.json");
$this->client->setDescription($description);
}
public function create(StuffEntity $entity)
{
$postArray = array('postParam1' => $entity->getPostParam1(), 'postParam2' => $entity->getPostParam2());
$params = array(
'parameters' => json_encode($postArray),
'accountid' => $entity->getAccountId(),
'product' => $entity->getProduct()
);
$response = $this->performCall('createStuff', $params);
$locationAsArray = $response->getHeader('location')->raw();
$location = $locationAsArray[0];
return $location;
}
private function performCall($operationName, $parameters)
{
$command = $this->client->getCommand($operationName, $parameters);
$command->prepare();
$response = $this->client->execute($command);
return $response;
}
}
MySubscriber
class MySubscriber implements Symfony\Component\EventDispatcher\EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array('request.success' => 'onRequestSuccess');
}
public function onRequestSuccess(Event $event)
{
var_dump($event['request']->getPostFields());
}
}
使用上述类的代码是:
$client = new Guzzle\Service\Client();
$subscriber = new MySubscriber;
$client->addSubscriber($subscriber);
$api = new StuffApi($client, 'http://url.to.webservice.com/');
$x = new StuffEntity();
$x->setAccountId(654143);
$x->setProduct('theproduct');
$x->setPostParam1(1);
$x->setPostParam2('989054MNBNMO5490BMN54');
$result = $api->createStuff($x);
好的,现在的问题是,由于我在onRequestSuccess
方法中对POST
字段执行了var\u dump
,我希望看到上面设置的值1
和“989054MNBNMO5490BMN54”
相反,我得到的是:
object (Guzzle\Http\QueryString) [35]
protected 'fieldSeparator' => string '&' (length=1)
protected 'valueSeparator' => string '=' (length=1)
protected 'urlEncode' => string 'RFC 3986' (length=8)
protected 'aggregator' => null
protected 'data' =>
array (size=0)
empty
我确实需要在onRequestSuccess
方法中访问请求中使用的POST信息。为什么它不在那里?还是我想用错误的方式得到它
先谢谢你
PS:我使用的Guzzle版本是3.8.1你试过var\u dump($\u POST)吗?@JochenJung没有,但这不是有点。。。丑陋?我想把它做好,我怀疑Guzzle确实有某种机制可以让我做到这一点,我只是没有找到它。。。然而,@JochenJung-Plus,它不会工作,因为我实际上没有给我的PHP脚本提供POST参数;相反,Guzzle构建它们并在幕后发出请求。因此,
var\u dump($\u POST)
将始终为空。