0

Im making this API adapter to POST data our OMS (Order Management System). And I keep getting this error. I dunno if it's really an error because the adapter is connected. the POSTing is the problem. I'm using JSON and cURL to pass data to be updated. So here's my code:

    $data = array(
       'package' => array(
            'tracking_number'  =>  '735897086',
    'package_status'    => 'failed',
            'failed_reason'     => 'other1',
            'update_at'        => '2013-11-22 09:58:39'
        )
    );

and this is how I POST it.

    $postdata = "apikey=$apikey&method=$method&data=$check";
    $ch = curl_init();

    //SSL verification fixed with this two codes
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

    curl_setopt_array(
        $ch,
        array(

            CURLOPT_URL             => $url.'/webservice/',
            CURLOPT_FOLLOWLOCATION  => true,
            CURLOPT_VERBOSE         => true,
    CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_CUSTOMREQUEST   => 'POST',
    CURLOPT_POSTFIELDS      => $postdata,
    CURLOPT_HTTPHEADER      => array('Content-type:  application/x-www-form-urlencoded')
        )
    );

    $result = curl_exec($ch);

and this is my code to test the connection and check if the POSTing is success.

    if(curl_exec($ch) === false) { echo 'Curl error: ' . curl_error($ch); } else { echo 'Operation completed without any errors'; } 
    $result = curl_exec($ch);
    echo $result;

    curl_close($ch);

I don't really know why I keep getting the "INCORRECT PARAMETERS SENT TO SERVICE". I already reviewed the documentation, the parameters are right. :(

nicole101
  • 187
  • 3
  • 20
  • I think @Simon is right, you are passing $check while you initialized array with $data so you can use like this $postdata = "apikey=$apikey&method=$method&data=".http_build_query($data, '', '&'); – Siraj Khan Dec 03 '13 at 06:25
  • Please see this also http://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl , it should help for you. – Siraj Khan Dec 03 '13 at 06:25

2 Answers2

0

I do believe it is because your POST variables are an array within an array so, what you end up trying to do with your current approach is invalid as stated.

Prior to setting $data in CURL try running the following:

$data = http_build_query($data);

See the PHP definition of http_build_query for more details

Simon
  • 816
  • 2
  • 7
  • 16
0

I forgot to add this. I encode it to JSON that's why I use arrays.

$check=json_encode($data);
echo $check;
$postdata = "method=$method&data=$check&apikey=$apikey";
$ch = curl_init();

I echo it first before getting the response to check if it's encoded in JSON. then I got this error:

{"package":{"order_number":"200118788","package_number":"200118788-4274","tracking_number":"735897086","package_status":"failed","failed_reason":"other1","update_at":"2013-08-06 17:02:14"}}Operation completed without any errors{"OmsSuccessResponse":false,"message":"Incorrect parameters sent to service","package_status":null}

nicole101
  • 187
  • 3
  • 20