简体   繁体   English

php curl发布不起作用

[英]php curl post not working

My POST curl works from command line, but not from php. 我的POST curl可从命令行运行,但不能从php运行。 It's just not sending the POST data in PHP. 它只是不发送PHP中的POST数据。 (I already checked to see if it's rewriting as GET and it's not doing that either althought GET works if I use GET) (我已经检查过它是否被重写为GET,尽管我使用GET时GET也可以,但是它也没有这样做)

command line: 命令行:

curl -d "something=true" file.php

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

Please refer to: php curl post json 请参考: php curl post json

Don't set Content-Length yourself but allow libcurl to do that by itself to reduce the risk for mistakes. 不要自己设置Content-Length,而是允许libcurl自己执行此操作以减少出错的风险。

Then, you've added two headers without colons, using '=' instead, which probably confuse the receiving end. 然后,您添加了两个不带冒号的标头,而使用了'=',这可能会使接收端感到困惑。

See how to send post data with curl: 了解如何使用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;
}

And example 和例子

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

You need try! 您需要尝试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM