使用php CURL从http post获取内容正文

时间:2012-03-14 18:04:25

标签: php curl http-post

我正在尝试调试我试图从列表应用程序发送的http帖子。我已经能够从php CURL发送正确的帖子,它与我的drupal 7网站核心接口并上传图像。

为了让这个在我的lisp应用程序中工作,我真的需要看到我的http帖子的内容正文我能够通过这样的调用看到标题:

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

并且我的lisp应用程序中的标题看起来相同但我无法检查帖子的正文。我在网上搜索过,其他人也问过这个问题,但没有人发布回复。

我的http帖子的内容类型是:

application/x-www-form-urlencoded

我也尝试了许多http代理debuging工具,但他们只有http GET来获取我的php页面,但从来没有捕获一旦php代码执行后从服务器发送的get。

编辑:我添加了一个代码snipet,显示我实际上传图像文件的位置。

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

6 个答案:

答案 0 :(得分:20)

CURLOPT_VERBOSE实际上应该显示详细信息。如果您正在查找回复正文内容,也可以使用CURLOPT_RETURNTRANSFERcurl_exec()将返回回复正文。

如果你需要检查请求正文,CURLOPT_VERBOSE应该给你,但我不完全确定。

无论如何,good network sniffer应该透明地为您提供所有细节。

示例:

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

输出:

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

答案 1 :(得分:9)

只需将其发送到随机本地端口并收听即可。

# terminal 1
nc -l localhost 12345

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

答案 2 :(得分:6)

你很亲密:

PHP manual指示您必须在curl_setopt和curl_getinfo中调用常量CURLINFO_HEADER_OUT。

$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

<强>概要

  • 设置curl_setopt
  • 设置curl_getinfo
  • 在curl_exec
  • 之后调用curl_getinfo

答案 3 :(得分:4)

如果您正在讨论查看回复,如果您添加curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );,那么请求返回的文档应该从您致电curl_exec时返回。

如果你正在谈论查看你发送的postdata,那么你应该能够查看,因为你在PHP中设置了它。

编辑:发布文件,是吗? $file的内容是什么?我猜可能打电话给file_get_contents()

尝试这样的事情:

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

你不能只发送文件,你仍然需要一个postdata数组,为该文件分配一个键(所以你可以在PHP中访问$_FILES['upload'])。此外,@告诉cURL加载指定文件的内容并发送而不是字符串。

答案 4 :(得分:2)

我认为使用代理比使用PHP更好。我认为不可能从PHP CURL库中提取原始POST数据。

代理应该向您显示请求和响应内容

答案 5 :(得分:-1)

要获取标题,需要在调用curl_exec之前设置 CURLINFO_HEADER_OUT 标志。 然后使用curl_getinfo和相同的标志来获取curl_exec之后的标题。

如果您想查看帖子数据,请抓住您在 CURLOPT_POSTFIELDS

设置的值

例如:

$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";