eclipse31
eclipse31

Reputation: 396

Full HTTP Request from cURL in PHP

I'm trying to upload an image to a site via their API by trying to mimic a web form and posting the data using cURL. I'm looking to check what cURL is actually sending to the destination site, to ensure I've built the request correctly. I see you can use CURLOPT_VERBOSE to see what it's sending in the request header, but I'm looking to see the posted data, after

Content-Type: multipart/form-data; boundary=----------------------------91f22eea64e8

The data I'm posting is in $post and the request is

        $ch = curl_init();
        $opts = array(
            CURLOPT_POST => 1,
            CURLOPT_CONNECTTIMEOUT => 10,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible;)',
            CURLOPT_POSTFIELDS => $post,
            CURLOPT_URL => 'https://example.com/api',
            CURLOPT_HTTPHEADER => array('Expect:'),
            CURLOPT_VERBOSE => 1
        );
        curl_setopt_array($ch, $opts);
        $response = curl_exec($ch);

Thanks

Upvotes: 2

Views: 3267

Answers (1)

Rogus
Rogus

Reputation: 1270

I have handled this problem in the past by sending the curl data to myself first, to make sure I am sending the right stuff. The file on my own webserver that I would send to (most likely the very same webserver I am sending from to begin with) needs only this in a .php file: <?php echo file_get_contents('php://input'); ?> so set the URL to that file for your CURLOPT_URL in your current php script, then send it with echo $response; in your curl calling code to see what was sent.

See also: http://www.codediesel.com/php/reading-raw-post-data-in-php/ and http://php.net/manual/en/wrappers.php.php for extra details/examples.

Oh, although in re-reading your question I see that you are sending an image, so you have to send multipart/form-data and that doesn't work with the php://input stream, you have to use php://stdin for that, according to the PHP docs linked above. I expect it will work the same regardless.

Upvotes: 3

Related Questions