Getting content body from http post using php CURL

孤街浪徒 提交于 2019-11-29 01:18:17

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">
...

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

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

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.

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

To get the header the CURLINFO_HEADER_OUT flag needs to be set before curl_exec is called. Then use curl_getinfo with the same flag to get the header after curl_exec.

If you want to see the post data, grab the value you set at CURLOPT_POSTFIELDS

For example:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://example.com/webservice");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

curl_exec($ch);

$header = curl_getinfo($ch, CURLINFO_HEADER_OUT);

curl_close($ch);

echo "Request-Header:\r\n" . $header . "\r\n";
echo "Request-Body(URL Encoded):\r\n" . http_build_query($payload) . "\r\n";
echo "Request-Body(Json Encoded):\r\n" . json_encode($payload) . "\r\n";
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!