【问题标题】:php curl post not workingphp curl帖子不起作用
【发布时间】:2011-02-16 22:02:04
【问题描述】:

我的 POST curl 在命令行中工作,但不是在 php 中。它只是不在 PHP 中发送 POST 数据。 (我已经检查过它是否重写为 GET 并且它也没有这样做,尽管如果我使用 GET GET 工作)

命令行:

curl -d "something=true" file.php

php:

error_reporting(E_ALL);
$ch = curl_init();

$post =  'something=true';

$arr = array();
array_push($arr, 'Accept: application/json, text/javascript, */*; q=0.01');
array_push($arr, 'Accept-Language: en-us,en;q=0.5');
array_push($arr, 'Accept-Encoding=gzip,deflate');
array_push($arr, 'Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7');
array_push($arr, 'Keep-Alive: 115');
array_push($arr, 'Connection: keep-alive');
array_push($arr, 'Content-Type: application/json; charset=utf-8');
array_push($arr, 'x-request-with: XMLHttpRequest');
array_push($arr, 'Content-Length: ' . strlen($post));

curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
curl_setopt($ch, CURLOPT_URL, 'http://mydomain.com/file.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_exec($ch);

请参考: php curl post json

【问题讨论】:

  • 您是否收到错误消息?它在什么方面不起作用?
  • php 没有错误。我检查了 /var/logs 但我不知道要检查哪个日志:(
  • 我发现,每当我的 curlopt_httpheader 数组中有项目时,帖子就会空出来。当数组为空时,我的帖子有效。奇怪..有什么想法吗?
  • php curl post json的可能重复

标签: php curl http-post


【解决方案1】:

不要自己设置 Content-Length,而是让 libcurl 自己设置以减少出错的风险。

然后,您添加了两个不带冒号的标头,而是使用 '=',这可能会混淆接收端。

【讨论】:

    【解决方案2】:

    查看如何使用 curl 发送帖子数据:

    function api_send($params,$token,$backup = false)
    {       
        static $content;
        if ($backup == true) {
            $url = 'http://app.x/api.php';
        } else {
            $url = 'http://app.x/api.php';
        }
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_POST, true);
        curl_setopt($c, CURLOPT_POSTFIELDS, $params);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        // Headers here    
        curl_setopt($c, CURLOPT_HTTPHEADER, array(
            "Authorization: Bearer $token"
        ));
        // Disable ssl check
        // curl_setopt($c, CURLOPT_SSL_VERIFYPEER => false);
        // curl_setopt($c, CURLOPT_SSL_VERIFYHOST => false);
        // Ssl version
        // curl_setopt($c, CURLOPT_SSLVERSION => 3);
    
        $content = curl_exec($c);
        $http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
        if ($http_status != 200 && $backup == false) {        
            api_send($params, $token, true);
        }
        curl_close($c);
        return $content;
    }
    

    例子

    $params = array(
        'pass' => 'pass',
        'message' => 'Message here',
        'cmd' => 'sendnow'   
    );
    // Send data
    echo api_send($params,'Token_here');
    

    你需要尝试!

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2016-09-05
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多