简体   繁体   中英

How do I send parameters in an HTTP POST request with php?

I trying to send a POST request in php but the post fields are in the url.

 $project_no        = 1711;
 $group_name        = "name of group";

 $url = urlencode("http://api.example.org/v1/projects?auth_token=xsxxex-xxmczx-66mvmc-9133&project_no=" . $project_no . "&group_name=" . $group_name);

 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 $response = curl_exec($ch);
 curl_close($ch);

var_dump($response)

The code above returns a error that says

bool(false)

What am I doing wrong here?

You need to include the vars as postfields and send them as an additional curl parameter:

$data = array('project_no' => $project_no, 'group_name' => $group_name);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

If the application requires json, you can json_encode() the $data first.

$json = json_encode($data);

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

I don't know the specifics of your application but probably, the auth_token may need to be sent too as an authorization header:

$headers = array(
                'Authorization': 'xsxxex-xxmczx-66mvmc-9133',
                //etc);


    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM