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

Mik*_*012 20 php curl http-post

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

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

curl_setopt($curl,   CURLOPT_STDERR, $fp);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
Run Code Online (Sandbox Code Playgroud)

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

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

application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)

我也尝试了许多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);
Run Code Online (Sandbox Code Playgroud)

hak*_*kre 22

CURLOPT_VERBOSE应该实际显示细节.如果您正在寻找响应正文内容,您也可以使用CURLOPT_RETURNTRANSFER,curl_exec()然后返回响应正文.

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

无论如何,一个好的网络嗅探器应该透明地为您提供所有细节.

例:

$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;
Run Code Online (Sandbox Code Playgroud)

输出:

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">
...
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的所有回复.我试图获取请求的内容而不是响应.我需要看看整个帖子是如何格式化的,以便我可以在我的lisp应用程序中模拟它.您的示例显示了响应的正文,但在请求中(在您的示例中为GET,在我的情况下为POST)我从未看到正文.我假设在我的情况下身体将是我试图上传的图像文件转换成数据以及解释文件名等的某种内容 - 处置. (3认同)

Tgr*_*Tgr 9

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

# terminal 1
nc -l localhost 12345

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

  • 我一直在寻找 PHP 解决方案,但这很聪明,并向我展示了我需要看到的内容。谢谢。 (3认同)

mec*_*mdi 6

你很亲密:

PHP手册指示,你必须调用两个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
Run Code Online (Sandbox Code Playgroud)

概要

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

  • 这似乎只显示标题,问题是关于请求的正文 (5认同)