【问题标题】:Curl command and slim phpcurl命令和slim php
【发布时间】: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_POSTFIELDSvalue 是一个数组时,Curl 将Content-Type 设置为这个。 secure.php.net/manual/en/function.curl-setopt.php
  • PHP 版本是的。我建议从命令行版本中删除 -H 开关。

标签: php post curl command-line slim


【解决方案1】:

我遇到了同样的问题,问题是请求需要 SSL 证书。 我通过将此行添加到 cURL 参数来解决它。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 2013-08-25
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多