【发布时间】:2015-11-05 02:43:31
【问题描述】:
我想在命令行中通过 php curl 从 curl 发布数据和标题。我在命令行中使用它:
curl -X POST -H "API_KEY:1234" -H "Content-Type: application/json"
--data "url=http://example.com/storage/testing.pdf" http://fake.com/index.php/tp
我得到的输出: 1234
在 PHP 中:
<?php
$header = array();
$header[] = 'API_KEY:1234';
$postdata = array('url' => "http://example.com/storage/testing.pdf");
$ch = curl_init("localhost/fake.com/index.php/tp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
我得到的输出: 1234http://example.com/storage/testing.pdf
我在 SLIM php 框架中使用以下内容:
$app->map('/tp', function() use ($app) {
$postvar = $app->request->post('url');
$headers = $app->request->headers;
$apikey = $app->request->headers->get('API_KEY');
echo $apikey;
echo $postvar;
})->via('POST');
我不明白为什么从命令行发帖没有给我 SLIM 中的帖子变量。请帮忙。
【问题讨论】:
-
尝试删除 -H "Content-Type: application/json" 因为您实际上是在发布 application/x-www-form-urlencoded 数据。
-
其实他在发
multipart/form-data数据。当CURLOPT_POSTFIELDS的value是一个数组时,Curl 将Content-Type设置为这个。 secure.php.net/manual/en/function.curl-setopt.php -
PHP 版本是的。我建议从命令行版本中删除 -H 开关。
标签: php post curl command-line slim