Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ZF2 Curl不发送post值_Php_Zend Framework2 - Fatal编程技术网

Php ZF2 Curl不发送post值

Php ZF2 Curl不发送post值,php,zend-framework2,Php,Zend Framework2,我正在尝试使用本机Zend Framework 2 http\curl库,我可以让它将请求发送到远程应用程序,但我只是无法让它向其发布值 下面是我的代码,它显示了两个示例,第一个是使用本机PHP curl,它运行良好,第二个是使用ZF2 http\curl库,它不传递任何POST参数 示例1(本机PHP库) 示例2(ZF2库的使用) 有人能指出我在这方面的错误吗?我看过ZF2文档,但它们不是最全面的 您实际上不需要在Curl适配器上指定所有这些细节。这是ZF2为您所做的: $url=$postr

我正在尝试使用本机Zend Framework 2 http\curl库,我可以让它将请求发送到远程应用程序,但我只是无法让它向其发布值

下面是我的代码,它显示了两个示例,第一个是使用本机PHP curl,它运行良好,第二个是使用ZF2 http\curl库,它不传递任何POST参数

示例1(本机PHP库)

示例2(ZF2库的使用)


有人能指出我在这方面的错误吗?我看过ZF2文档,但它们不是最全面的

您实际上不需要在
Curl
适配器上指定所有这些细节。这是ZF2为您所做的:

$url=$postrl$波斯托里;
$postString=“username={$username}&password={$password}”;
$client=new\Zend\Http\client();
$client->setAdapter(new\Zend\Http\client\Adapter\Curl());
$request=new\Zend\Http\request();
$request->setUri($url);
$request->setMethod(\Zend\Http\request::METHOD\u POST);
$request->setContent($postString);
$response=$client->dispatch($request);
变量转储($response->getContent());

这是我用来解决此问题的解决方案

    $url = $postUrl . "" . $postUri;

    $request = new Request;
    $request->getHeaders()->addHeaders([
        'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
    ]);
    $request->setUri($url);
    $request->setMethod('POST'); //uncomment this if the POST is used
    $request->getPost()->set('username', $username);
    $request->getPost()->set('password', $password);

    $client = new Client;

    $client->setAdapter("Zend\Http\Client\Adapter\Curl");

    $response = $client->dispatch($request);

抱歉,回复所花的时间太长了。这会导致错误,因为
$request->setBody($postString)方法不存在。我的错,它是
setContent
。修正了答案。非常感谢,我最终选择了一个不同的解决方案。但这似乎也起到了作用。
    $url = $postUrl . "" . $postUri;

    $postString = "username={$username}&password={$password}";

    //Does not work using ZF2 method!
    $request = new Request;

    $request->setUri($url);
    $request->setMethod('POST');

    $adapter = new Curl;

    $adapter->setOptions([
        'curloptions' => [
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $postString,
            CURLOPT_HEADER => 1
        ]
    ]);

    $client = new Client;
    $client->setAdapter($adapter);

    $response = $client->dispatch($request);

    var_dump($response->getBody());
    $url = $postUrl . "" . $postUri;

    $request = new Request;
    $request->getHeaders()->addHeaders([
        'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
    ]);
    $request->setUri($url);
    $request->setMethod('POST'); //uncomment this if the POST is used
    $request->getPost()->set('username', $username);
    $request->getPost()->set('password', $password);

    $client = new Client;

    $client->setAdapter("Zend\Http\Client\Adapter\Curl");

    $response = $client->dispatch($request);