简体   繁体   中英

send post request to website via php (similar to xmlhttprequest)

I was wondering, in php is it possible to send a post request to another site without forms (just parameters) like xmlhttprequest in javascript? If so how would it be possible? i'd appreciate some examples. I think it's with curl, so in this case, how would i send the following parameters: name=bob&lastname=tim via post request to www.example.com using curl in php?

Try this:

$fields_string = "name=bob&lastname=tim";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "http://www.example.com");
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

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