Getting content body from http post using php CURL

59,699

Solution 1

CURLOPT_VERBOSE should actually show the details. If you're looking for the response body content, you can also use CURLOPT_RETURNTRANSFER, curl_exec() will then return the response body.

If you need to inspect the request body, CURLOPT_VERBOSE should give that to you but I'm not totally sure.

In any case, a good network sniffer should give you all the details transparently.

Example:

$curlOptions = array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_STDERR => $verbose = fopen('php://temp', 'rw+'),
    CURLOPT_FILETIME => TRUE,
);

$url = "http://stackoverflow.com/questions/tagged/java";
$handle = curl_init($url);
curl_setopt_array($handle, $curlOptions);
$content = curl_exec($handle);
echo "Verbose information:\n", !rewind($verbose), stream_get_contents($verbose), "\n";
curl_close($handle);
echo $content;

Output:

Verbose information:
* About to connect() to stackoverflow.com port 80 (#0)
*   Trying 64.34.119.12...
* connected
* Connected to stackoverflow.com (64.34.119.12) port 80 (#0)
> GET /questions/tagged/java HTTP/1.1
Host: stackoverflow.com
Accept: */*

< HTTP/1.1 200 OK
< Cache-Control: private
< Content-Type: text/html; charset=utf-8
< Date: Wed, 14 Mar 2012 19:27:53 GMT
< Content-Length: 59110
< 
* Connection #0 to host stackoverflow.com left intact

<!DOCTYPE html>
<html>



<head>



    <title>Newest &#39;java&#39; Questions - Stack Overflow</title>
    <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">
    <link rel="apple-touch-icon" href="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png">
    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
...

Solution 2

Just send it to a random local port and listen on it.

# terminal 1
nc -l localhost 12345

# terminal 2
php -e
<?php
$curl = curl_init('http://localhost:12345');
// etc

Solution 3

You were close:

The PHP manual instructs that you must call the constant CURLINFO_HEADER_OUT in both curl_setopt and curl_getinfo.

$ch = curl_init($url);
... other curl options ...
curl_setopt($ch,CURLINFO_HEADER_OUT,true);

curl_exec(ch);
//Call curl_getinfo(*args) after curl_exec(*args) otherwise the output will be NULL.
$header_info = curl_getinfo($ch,CURLINFO_HEADER_OUT); //Where $header_info contains the HTTP Request information

Synopsis

  • Set curl_setopt
  • Set curl_getinfo
  • Call curl_getinfo after curl_exec

Solution 4

If you're talking about viewing the response, if you add curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );, then the document returned by the request should be returned from your call to curl_exec.

If you're talking about viewing the postdata you are sending, well, you should be able to view that anyway since you're setting that in your PHP.

EDIT: Posting a file, eh? What is the content of $file? I'm guessing probably a call to file_get_contents()?

Try something like this:

$postdata = array( 'upload' => '@/path/to/upload/file.ext' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $postdata );

You can't just send the file, you still need a postdata array that assigns a key to that file (so you can access in PHP as $_FILES['upload']). Also, the @ tells cURL to load the contents of the specified file and send that instead of the string.

Solution 5

I think you're better off doing this with a proxy than in the PHP. I don't think it's possible to pull the raw POST data from the PHP CURL library.

A proxy should show you the request and response contents

Share:
59,699

Related videos on Youtube

Mike2012
Author by

Mike2012

Updated on July 13, 2020

Comments

  • Mike2012
    Mike2012 almost 4 years

    I am trying to debug an http post the I am trying to send from list application. I have been able to send the correct post from php CURL which corectly interfaces with my drupal 7 website and uploads an image.

    In order to get this to work in my lisp application I really need to see the content body of my http post I have been able to see the headers using a call like this:

    curl_setopt($curl,   CURLOPT_STDERR, $fp);
    curl_setopt($curl, CURLOPT_VERBOSE, 1);
    

    and the headers look the same in my lisp application but I have been unable to examine the body of the post. I have searched online and other people have asked this question but no one posted a response.

    The content type of my http post is:

    application/x-www-form-urlencoded
    

    I have also tried many http proxy debuging tools but they only ever the http GET to get my php page but never capture the get sent from server once the php code is executed.

    EDIT: I have added a code snipet showing where I actually upload the image file.

    // file
    $file = array(
      'filesize' => filesize($filename),
      'filename' => basename($filename),
      'file' => base64_encode(file_get_contents($filename)),
      'uid' => $logged_user->user->uid,
    );
    
    $file = http_build_query($file);
    
    // REST Server URL for file upload
    $request_url = $services_url . '/file';
    
    // cURL
    $curl = curl_init($request_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
    curl_setopt($curl,   CURLOPT_STDERR, $fp);
    curl_setopt($curl, CURLOPT_VERBOSE, 1);
    curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
    curl_setopt($curl, CURLOPT_POSTFIELDS, $file); // Set POST data
    curl_setopt($curl, CURLOPT_HEADER, FALSE);  // Ask to not return Header
    curl_setopt($curl, CURLOPT_COOKIE, "$cookie_session"); // use the previously saved session
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
    curl_setopt_array($curl, array(CURLINFO_HEADER_OUT => true) );
    $response = curl_exec($curl);
    
    • user1854496
      user1854496 about 9 years
      I don't suppose you found an answer to this?
    • Bobot
      Bobot about 9 years
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  • Mike2012
    Mike2012 about 12 years
    Hey Ryan thanks for your response. I am trying to view the postdata but I am not directly setting it. I am trying to upload a file with this post and I set that with a call like this: curl_setopt($curl, CURLOPT_POSTFIELDS, $file); is there someway I can still print this?
  • Mike2012
    Mike2012 about 12 years
    Thanks for your response James. I have tried quite a few proxies but they only seem to catch the GET to load my php page, never the POST from the php code being executed.
  • Mike2012
    Mike2012 about 12 years
    CURLOPT_VERBOSE only seems to show me the header is there some other flag I would need to set?
  • Mike2012
    Mike2012 about 12 years
    Wireshark only seems to show me the GETs being sent to load the my php page but not POST being sent from the PHP code.
  • hakre
    hakre about 12 years
    @Mike2012: Yes, CURLOPT_VERBOSE does not return the response body. I've added an example that shows you how to retrieve the response body, that's CURLOPT_RETURNTRANSFER. If that's not what you need, then please specify what you need. And wireshark shows you everything that goes over the wire.
  • kitti
    kitti about 12 years
    @Mike2012 Ok, I've updated my answer. Let me know if my assumptions are incorrect, and post code if necessary.
  • Mike2012
    Mike2012 about 12 years
    Thanks for all your responses hakre. I am trying to get the content of the request not the response. I need to see how the entire post is formatted so that I can emulated this in my lisp application. Your example shows the body of the response but in the request (in your example a GET, in my case a POST) I never see the body. I assume the body in my case would be the image file I am trying to upload converted into data along with some sort of content-disposition explaining the filename and such.
  • Mike2012
    Mike2012 about 12 years
    I have also scoured the wc3 website looking for documentation on how to format a raw http post but I have nothing. Sorry for my noobishness but this is my first attempt at doing anything like this.
  • hakre
    hakre about 12 years
    Okay, you need the request body. AFAIK curl does not offer any interface to that, the curl commandline client can do this (which might not be suitable for you), see as well Echo curl request header & body without sending it? - The specification you are looking for can be found here: application/x-www-form-urlencoded (W3C).
  • Mike2012
    Mike2012 about 12 years
    thanks again. I am uploading an image file and I have updated my example to include a code snippet that shows how I am doing that. In your example it seems the tag upload is associated with the file and then used as an accessor to $_FILES, but in my case I don't quite set it up that way. Could I still do something similar with my code? Also, in my example would this file data be the entire contents of the postdata or would there be some sort of header specifying the file name and such?
  • kitti
    kitti about 12 years
    @Mike2012 Probably if you just replace 'file' => base64_encode(file_get_contents($filename)), with 'file' => '@' . $filename, it should work. Access in PHP as $_FILES['file'].
  • Mike2012
    Mike2012 about 12 years
    this gives me an Undefined index: file in /Applications/MAMP/htdocs/upload.php on line 94 error.
  • ChristoKiwi
    ChristoKiwi over 9 years
    That only seems to show the headers, the question is regarding the body of the request
  • par
    par over 6 years
    I had to do nc -l localhost -p 12345
  • Matthew Poer
    Matthew Poer over 3 years
    I was looking for a PHP solution but this was clever and showed me what I needed to see. Thanks.
  • AnrDaemon
    AnrDaemon over 3 years
    That won't be the data curl sent in request, only your best guess about them.
  • AnrDaemon
    AnrDaemon almost 3 years
    At that rate, you could run PHP's integrated webserver with a simple router script that doesn't even need curl to function.