1

I am trying to convert this cURL script to use it with PHP and Guzzle. I have been able to set the cookie as shown below but I cannot send the xml file I need to afterwards.

cURL Script

# 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

This is what I have that sets the cookie. I am not sure what the next part is to actually send the xml file I have.

        $client = new \GuzzleHttp\Client();

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

I have also tried this. However, I get an error message:

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);

Update

    $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);
Lynx
  • 1,462
  • 5
  • 32
  • 59

1 Answers1

1

It looks like you are calling send() from the wrong class. send() is a method of \GuzzleHttp\Client. So you need to do $client->send() instead.

However, $client->post() sends the request as soon as it creates it. If you want to use send() then you'll need to replace post() with createRequest(), as seen here: http://guzzle.readthedocs.org/en/latest/clients.html#creating-requests

You'll also have problems with the call to fopen() which returns a file handle instead of the contents. Try file_get_contents() instead.

Edit:

In order to set the auth cookie, you'll need a cookie jar. Try the following:

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

Using the same 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);
Jeremiah Winsley
  • 2,537
  • 18
  • 30
  • OK, I updated the question to show what I have so far. It returns the login screen similar to the question you helped me with the other day. http://stackoverflow.com/questions/25089960/how-to-get-passed-login-screen-on-guzzle-call/25120844?noredirect=1#comment39103225_25120844 Where can I set that cookie, true so that it can bypass the login screen? – Lynx Aug 07 '14 at 16:56
  • it was the same error i got when I tried this. syntax error, unexpected ''cookies'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' – Lynx Aug 07 '14 at 17:07
  • Sorry, should have saw that myself. It's still not working however. Still showing the same message. Requiring login – Lynx Aug 07 '14 at 17:14
  • OK, i was able to get past the login screen I forgot to set cookies to true on the login page. Now, however the file will not upload it just goes to the import.php page itself – Lynx Aug 07 '14 at 17:33
  • Make sure you're submitting to the correct page, then. Assuming it's the same site, `http://xxxxxxxx/WMS/weborders/importXMLOrder.php`. The xml should be loaded to the post variable `import` rather than `file_filed`, according to their API guide. Also, `file_get_contents()` doesn't take a second parameter. Updated to reflect that. – Jeremiah Winsley Aug 07 '14 at 17:43
  • looks like that did it! I was successfully able to send it over. It only worked once and is no longer working but I may be able to figure out the reason why. Thanks a million for the help! – Lynx Aug 07 '14 at 18:31
  • you can use $response->getBody()->getContents() – Dinesh Rabara Sep 23 '15 at 15:04