PHP将帖子数据发送到URL并从该网页获得响应


PHP Send post data to URL and get response from that webpage

是否可以将POST数据发送到URL,并从该网页获得响应,它是JSON格式的,并将该数据用于不同的用途。如果可能,怎么做?

谢谢:)

编辑:我有一个表单发送到另一台服务器上的另一个php脚本,但我想从该脚本中获得响应,并在我的脚本中使用它。另一个发送的数据是JSON格式的,但这已经不是问题了。

使用php-ccurl可以实现以下

$url = "http://google.com/";
$aParameter = array('id'=>1,'name'=>'test'); //parameters to be sent
$params = json_encode($aParameter); //convert param to json string
//headers to be sent optional if no header required skip this and remove curlopt_httpheader thing from curl call
$aHeaders = array(
        'Client-Secret'=>'XXXXX',
        'Authorization'=>'xxxxxx',
        'Content-Type'=>'Content-Type:application/json',
        'accept'=>'accept:application/json'
    );

$c = curl_init();
curl_setopt($c, CURLOPT_USERAGENT,  'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0'); // empty user agents probably not accepted
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_AUTOREFERER,    1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($c, CURLOPT_HTTPHEADER, $aHeaders);
curl_setopt($c, CURLOPT_URL, $url );
curl_setopt($c, CURLOPT_REFERER, $url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c,CURLOPT_POSTFIELDS,$params);
$sResponse[$key] = curl_exec($c);