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

PHP将post数据发送到URL并从该网页获得响应,php,json,post,Php,Json,Post,是否可以将POST数据发送到URL并从该网页返回响应,它是JSON格式的,并将该数据用于其他用途。 如果可能,怎么做 谢谢:) 编辑:我有一个表单发送到另一台服务器上的另一个php脚本,但我想从该脚本获得响应并在脚本中使用它。另一方发送的数据是JSON格式的,但这不再是一个问题。使用php curl,您可以通过以下方式实现 $url = "http://google.com/"; $aParameter = array('id'=>1,'name'=>'test'); //para

是否可以将POST数据发送到URL并从该网页返回响应,它是JSON格式的,并将该数据用于其他用途。 如果可能,怎么做

谢谢:)


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

使用php curl,您可以通过以下方式实现

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

是的,这是可能的,谷歌如jquery和ajaxYes,这是可能的。但是,如果您能够解释代码的具体位置,这将有助于我们更好地理解您的问题。如果您不清楚
POST
请求是如何工作的,以及我们如何创建和发送
JSON
响应,我建议用谷歌搜索一下。您将获得许多有用的教程来入门。对于PHP,请查看
cURL
函数。如果您打算从页面的javascript部分发送它,请阅读
AJAX
;如果要从页面的php部分发送,请查看
cURL
。请参阅(使用翻译工具)