0

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

Community
  • 1
  • 1
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • Do you get an error? In what way is it not working? – Cfreak Feb 16 '11 at 22:04
  • no error from php. I checked /var/logs but I didn't know which log to check :( – Jacksonkr Feb 16 '11 at 22:05
  • I found that whenever there are items in my curlopt_httpheader array, the post comes up empty. when the array is empty, my post works. odd.. any ideas? – Jacksonkr Feb 16 '11 at 23:41
  • possible duplicate of [php curl post json](http://stackoverflow.com/questions/5023705/php-curl-post-json) – j0k Mar 26 '13 at 08:16

2 Answers2

0

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.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
0

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!

Maxiu
  • 1