【问题标题】:PHP Send post data to URL and get response from that webpagePHP 将发布数据发送到 URL 并从该网页获取响应
【发布时间】:2014-10-27 09:07:13
【问题描述】:

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

谢谢:)

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

【问题讨论】:

  • 是的,这是可能的,例如 google jquery 和 ajax
  • 是的,有可能。但是,当您可以解释就代码而言究竟卡在哪里时,这将有助于我们更好地理解您的问题。如果您不清楚POST 请求的工作方式以及我们如何创建和发送JSON 响应,我建议您使用谷歌搜索。您将获得很多有用的教程来帮助您入门。
  • 对于 PHP,请查看 cURL 函数。
  • 如果您打算从页面的 javascript 部分发送它,请在 AJAX 上阅读一下;如果您打算从页面的 php 部分发送它,请查看cURL
  • 参考my article(使用翻译工具)

标签: php json post


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多