How to POST JSON Data With PHP cURL?

前端 未结 7 2189
失恋的感觉
失恋的感觉 2020-11-22 10:02

Here is my code,

$url = \'url_to_post\';
$data = array(
    \"first_name\" => \"First name\",
    \"last_name\" => \"last name\",
    \"email\"=>\"e         


        
7条回答
  •  清酒与你
    2020-11-22 10:23

    $url = 'url_to_post';
    $data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" =>  "Mother","last_name" =>  "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
    
    $postdata = json_encode($data);
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($ch);
    curl_close($ch);
    print_r ($result);
    

    This code worked for me. You can try...

提交回复
热议问题