简体   繁体   English

通过php将请求发送到网站(类似于xmlhttprequest)

[英]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? 我想知道,在php中是否可以将发布请求发送到另一个站点,而无需使用javascript中的xmlhttprequest这样的形式(只是参数)? 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? 我认为它带有curl,因此在这种情况下,我将如何通过php中的curl通过发帖请求将以下参数发送到www.example.com:name = bob&lastname = tim?

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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