转换cURL为Guzzle发送XML文件


convert cURL for Guzzle to send XML file

我正试图将这个cURL脚本与PHP和Guzzle一起使用。我已经能够设置cookie如下所示,但我不能发送我需要之后的xml文件。

<

旋度脚本/strong>

# First, we need to get the cookie
curl [-k] –dump-header <header_file> -F “action=login” -F “username=<username>” -F “password=<password>” https://<website_URL>
# Then, we can use that cookie to upload our orders
# XML Order Upload
curl -b <header_file> -F “import=@<order_file>” http://<website_URL>/importXMLOrder.php

这是我设置cookie的内容。我不确定下一部分实际发送xml文件是什么。

        $client = new 'GuzzleHttp'Client();
    $response = $client->post('http://website/login.php', array(
            'body' => array(
                'username' => 'xxxxx',
                'password' => 'xxxxxx'
            ))
    );

我也试过这个。但是,我得到一个错误消息:

Call to undefined method GuzzleHttp'Message'Response::send()

    $request = $client->post('http://website.com/import.php', array(
        'body' => array(
            'file_filed' => fopen('orders.xml', 'r')
        )));
    $response = $request->send();
    $data = $response->xml();
    print_r($data);

    $request = $client->createRequest('POST','http://website.com/import.php', array(
        'body' => array(
            'file_filed' => file_get_contents('orders.xml', 'r')
        )
    ));
    $response = $client->send($request);
    //var_dump($response); die;
    $data = $response->xml();
    echo '<pre>';
    print_r($data);

看起来您从错误的类调用send()send()'GuzzleHttp'Client的一种方法。所以你需要用$client->send()代替。

然而,$client->post()在创建请求后立即发送请求。如果你想使用send(),那么你需要用createRequest()替换post(),如下所示:http://guzzle.readthedocs.org/en/latest/clients.html#creating-requests

调用fopen()也会有问题,它返回一个文件句柄而不是内容。试试file_get_contents()吧。

编辑:

为了设置验证cookie,您需要一个cookie罐。试试以下命令:

$client = new 'GuzzleHttp'Client();
$auth = $client->post('http://website/login.php', array(
        'body' => array(
            'username' => 'xxxxx',
            'password' => 'xxxxxx'
        ),
        'cookies' => true
        ));

使用相同的Client:

$request = $client->createRequest('POST','http://website.com/import.php', array(
    'body' => array(
        'file_filed' => file_get_contents('orders.xml')
    ),
    'cookies' => true
    ));
$response = $client->send($request);
//var_dump($response); die;
$data = $response->xml();
echo '<pre>';
print_r($data);