使用php CURL从http post获取内容正文 [英] Getting content body from http post using php CURL

查看:404
本文介绍了使用php CURL从http post获取内容正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

为了让我在我的lisp应用程序中工作,我真的需要看到我的http post的内容正文我已经能够看到头使用调用像这:

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

并且标题在我的lisp应用程序中看起来一样,但我无法检查职位。我在网上搜索,其他人都提出这个问题,但没有人发表回应。

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.

我的http文章的内容类型是:

The content type of my http post is:

application/x-www-form-urlencoded

我也尝试过许多http代理调试工具, http GET获取我的PHP页面,但从来没有捕获从服务器发送一旦PHP代码执行。

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.

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

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


推荐答案

CURLOPT_VERBOSE 应该实际显示详细信息。如果您要查找响应体内容,还可以使用 CURLOPT_RETURNTRANSFER curl_exec() 会返回

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.

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

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

在任何情况下,良好的网络嗅探器应该

示例:

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

这篇关于使用php CURL从http post获取内容正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆