data-binary parameter in cURL
Asked Answered
W

2

7

I have to send data-binary parameter through cURL in php.
This is the command: curl -D - -u user:password -X PUT -H "Content-Type: text/plain" --data-binary "data-id=2010-10-01_15-15-53" https://someurl. In the console this works, now I have to do it in php.

This is the code I have:

    $this->_curl = curl_init();
    curl_setopt($this->_curl, CURLOPT_USERPWD, $this->_loginUser . ":" . $this->_loginPassword);
    curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->_curl, CURLOPT_HEADER, 1);
    curl_setopt($this->_curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($this->_curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($this->_curl, CURLOPT_URL, $this->_serviceUrl);//https://someurl
    curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
    curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($this->_curl, CURLOPT_POSTFIELDS, array('data-id' => $dataId));//d'2010-10-01_15-15-53'

    $response = curl_exec($this->_curl);
    //$response = HTTP/1.1 201 Created
    curl_close($this->_curl);

the call is accepted by the server, but it doesn't recognize the data-id parameter:

No data-id property defined in trigger 2010-10-01_15-15-53

Any idea what I'm missing?

Wattenberg answered 27/6, 2013 at 13:55 Comment(2)
Did you figure this out?Optative
I assume this is for surescripts integration. Do you have the basic connection setup with them to send and receive basic request with CURL / PHP?Optative
J
7

You need to convert your string to stream first.

You can simply do it with this piece of code.

$YourString = 'data-id=2010-10-01_15-15-53';
$stream = fopen('php://memory','r+');
fwrite($stream, $YourString );
$dataLength = ftell($stream);
rewind($stream);

Then having your stream, you can send it using curl.

$curl = curl_init();
curl_setopt_array( $curl,
        array( CURLOPT_CUSTOMREQUEST => 'PUT'
        , CURLOPT_URL => 'https://someurl'
        , CURLOPT_HTTPHEADER => array(
            'Content-Type: text/plain'
        )
        , CURLOPT_RETURNTRANSFER => 1                     // means output will be a return value from curl_exec() instead of simply echoed
        , CURLOPT_TIMEOUT => 15                           // max seconds to wait
        , CURLOPT_FOLLOWLOCATION => 0                     // don't follow any Location headers, use only the CURLOPT_URL, this is for security
        , CURLOPT_FAILONERROR => 0                        // do not fail verbosely fi the http_code is an error, this is for security
        , CURLOPT_SSL_VERIFYPEER => 1                     // do verify the SSL of CURLOPT_URL, this is for security
        , CURLOPT_VERBOSE => 0                            // don't output verbosely to stderr, this is for security
        , CURLOPT_INFILE => $stream
        , CURLOPT_INFILESIZE => $dataLength
        , CURLOPT_UPLOAD => 1
        ) );

$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo($response.'<br/>');
echo($http_code.'<br/>');

This should work. The lines that help you are highlighted below:

CURLOPT_INFILE => $stream

CURLOPT_INFILESIZE => $dataLength

CURLOPT_UPLOAD => 1

Jehius answered 6/12, 2015 at 16:41 Comment(0)
D
0

Why do you want pass text plain as binary?

--data-binary "data-id=2010-10-01_15-15-53"

In this line you say that you want transfer plain text, not binary:

curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));

In any case I think your main problem is to use PUT method. (https://mcmap.net/q/36141/-using-put-method-in-html-form)

I recommend you to use POST method in this way:

curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array(
    'Accept: */*',
    'Connection: Keep-Alive',
    // this allows you transfer binary data through POST
    'Content-type: multipart/form-data'
    ));
curl_setopt($this->_curl, CURLOPT_POST, 1);
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, 
    http_build_query(array('data-id' => $dataId)));

To easily transfer binary data via curl/php, check this: https://mcmap.net/q/402411/-post-a-file-string-using-curl-in-php

Dimidiate answered 27/6, 2013 at 18:23 Comment(1)
the text as binary and the PUT method are the service I am communicating with's requisites: if you look at the command I am trying to php-ize you'll see that it sets the content as plain text and pass the data-id as binary. I have just tried your recommendation with POST but then the call is not accepted by the server. thanx anywayWattenberg

© 2022 - 2024 — McMap. All rights reserved.