【问题标题】:URL call in PHP [closed]PHP中的URL调用[关闭]
【发布时间】:2013-02-22 18:53:37
【问题描述】:

我在 PHP 中使用 API。 我必须将数据推送到这个 API 中。例如,如果我去http://www.api.com/api.php?data=mydata,我将“mydata”推送到api。

事实上,我想使用 PHP 在 URL 栏中输入数据(这可能非常好,哈哈)

提前致谢!

【问题讨论】:

  • 完全不清楚你想要实现什么以及你已经尝试过什么......
  • 尝试将其添加到第一个 php 代码行之前:$_GET["data"] = "mydata";。请告诉我它是否有效!
  • 你可以使用curl

标签: php web-services api http web


【解决方案1】:

您是否尝试查看 cURL? http://php.net/manual/en/book.curl.php

示例:

$ch = curl_init();// init curl
curl_setopt($ch, CURLOPT_URL,"http://www.api.com/api.php");
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
curl_setopt($ch, CURLOPT_POSTFIELDS,"data=mydata&foo=bar");// post data

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// gives you a response from the server

$response = curl_exec ($ch);// response it ouputed in the response var

curl_close ($ch);// close curl connection

【讨论】:

    【解决方案2】:

    您需要发布数据吗?查看 http_post_data :

    http://php.net/manual/en/function.http-post-data.php

    【讨论】:

    • get没问题,那个app也一样
    【解决方案3】:

    你需要这个吗?

    $URL = "http://www.api.com/api.php?data=mydata";
    $data = file_get_contents($URL);
    

    数据现在包含响应。

    【讨论】:

    • 我在错误的 url 上提出了请求,伟大的 Job Clément 哈哈,我总是遇到 404 错误。现在好了 :-) 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多