php 使用php CURL从http post获取内容正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9707551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting content body from http post using php CURL
提问by Mike2012
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.
我正在尝试调试我试图从列表应用程序发送的 http 帖子。我已经能够从 php CURL 发送正确的帖子,它与我的 drupal 7 网站正确连接并上传图像。
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:
为了让它在我的 lisp 应用程序中工作,我真的需要查看我的 http 帖子的内容主体,我已经能够使用这样的调用来查看标题:
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.
并且标题在我的 lisp 应用程序中看起来相同,但我无法检查帖子的正文。我在网上搜索过,其他人也问过这个问题,但没有人回复。
The content type of my http post is:
我的 http 帖子的内容类型是:
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.
我也尝试过许多 http 代理调试工具,但它们只使用 http GET 来获取我的 php 页面,但一旦执行 php 代码,就永远不会捕获从服务器发送的 get。
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);
回答by hakre
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
实际上应该显示细节。如果您要查找响应正文内容,也可以使用CURLOPT_RETURNTRANSFER
,curl_exec()
将返回响应正文。
If you need to inspect the request body, CURLOPT_VERBOSE
should give that to you but I'm not totally sure.
如果您需要检查请求正文,CURLOPT_VERBOSE
应该将其提供给您,但我不完全确定。
In any case, a good network sniffershould 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 'java' 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">
...
回答by Tgr
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
回答by mechimdi
You were close:
你很接近:
The PHP manualinstructs that you must call the constant CURLINFO_HEADER_OUT in both curl_setopt and curl_getinfo.
在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
Synopsis
概要
- Set curl_setopt
- Set curl_getinfo
- Call curl_getinfo after curl_exec
- 设置 curl_setopt
- 设置 curl_getinfo
- 在 curl_exec 之后调用 curl_getinfo
回答by Ryan P
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
.
如果您正在谈论查看响应,如果您添加curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
,则请求返回的文档应该从您对 的调用返回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.
如果您正在谈论查看您发送的 postdata,那么您应该能够查看它,因为您是在 PHP 中设置的。
EDIT: Posting a file, eh? What is the content of $file
? I'm guessing probably a call to file_get_contents()
?
编辑:发布文件,嗯?的内容是$file
什么?我猜可能是打电话给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.
你不能只发送文件,你仍然需要一个 postdata 数组来为该文件分配一个键(这样你就可以在 PHP 中访问$_FILES['upload']
)。此外,@
告诉 cURL 加载指定文件的内容并发送该内容而不是字符串。
回答by James C
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.
我认为你最好使用代理而不是 PHP。我认为不可能从 PHP CURL 库中提取原始 POST 数据。
A proxy should show you the request and response contents
代理应该向您显示请求和响应内容
回答by Angrist
To get the header the CURLINFO_HEADER_OUTflag needs to be set before curl_exec is called. Then use curl_getinfo with the same flag to get the header after curl_exec.
要获取标头,需要在调用 curl_exec 之前设置CURLINFO_HEADER_OUT标志。然后使用具有相同标志的 curl_getinfo 来获取 curl_exec 之后的标头。
If you want to see the post data, grab the value you set at CURLOPT_POSTFIELDS
如果您想查看帖子数据,请获取您在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";