POST request with JSON body

后端 未结 5 1443
渐次进展
渐次进展 2020-11-28 20:27

I would like to add a post to a Blogger blog via PHP. Google provided the example below. How to use that with PHP?

You can add a post for a blog by se

5条回答
  •  野性不改
    2020-11-28 21:01

     "MUMBAI",
        "LOCATION" => "NA",
        "STORE" => "AMAZON"));
    // json encode data
    $authToken = "xxxxxxxxxx";
    $data_string = json_encode($data); 
    // set up the curl resource
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://domainyouhaveapi.com");   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type:application/json',       
        'Content-Length: ' . strlen($data_string) ,
        'API-TOKEN-KEY:'.$authToken ));   // API-TOKEN-KEY is keyword so change according to ur key word. like authorization 
    // execute the request
    $output = curl_exec($ch);
    //echo $output;
    // Check for errors
    if($output === FALSE){
        die(curl_error($ch));
    }
    echo($output) . PHP_EOL;
    // close curl resource to free up system resources
    curl_close($ch);
    

提交回复
热议问题