简体   繁体   中英

php curl post not working

My POST curl works from command line, but not from php. It's just not sending the POST data in PHP. (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)

command line:

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

Please refer to: php curl post json

Don't set Content-Length yourself but allow libcurl to do that by itself to reduce the risk for mistakes.

Then, you've added two headers without colons, using '=' instead, which probably confuse the receiving end.

See how to send post data with 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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