1

I been trying to pass data using post and curl in php and this is the code that will receive the data

<?php
# required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

# include database and object files
require_once "vendor/autoload.php";

use \Database\Db as Db;
use \Objects\Leads as Leads;

# get database connection
$database = new Db();
$database->Connect(
  getenv('DB_CONNECTION'),
  getenv('DB_DATABASE'),
  getenv('DB_USERNAME'),
  getenv('DB_PASSWORD'),
  getenv('DB_HOST')
);

# prepare product object
$leads = new Leads($database);

# get id of product to be edited
$fp = fopen('php://input', 'r');
$data = stream_get_contents($fp);

echo "<pre>";
print_r($rawData);
echo "</pre>";

and this is the code that send data

    $param = [
      'id'         => $result['id'],
      'transfered' => true
    ];

    $param = json_encode($param);
    $r = self::postSubmit($this->url . 'store.php', $param);
    dump($r);

and the functions code

  public function postSubmit($url, $param)
  {
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $param);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

    return curl_exec($ch);
  }

but when i try to run the code to see the result

the result just "" and in the database nothing has changed

Am i doing wrong with the code?

1 Answer 1

1

Maybe it's not the main reason, but also a problem for sure:

$param = json_encode($param);

Either remove that line from code OR change it to

$param = http_build_query($param);

because CURLOPT_POSTFIELDS don't supports json - it supports urlencoded string or plain array. I would recommend just plain array - no need to bother with type conversion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.