让我开始使用 fopen/curl 请求的非常快速的示例

发布于 2024-11-24 02:01:21 字数 402 浏览 0 评论 0原文

我希望开始从 comindwork 中提取数据,但我以前从未使用过任何网络服务,所以我真的不知道如何开始。

有人建议我可以使用 fopen 拉回数据或发送curl 请求,但我不知道是哪一个。

我正在使用这个 api:

项目 取回 获取所有项目

GET /projects.xml
GET|POST /project/list 

获取一个项目

GET /projects/{project_id}.xml
GET|POST /project/show/{project_id}
GET /projects/{project_id} 

哪个项目可以完成这项工作,您能否提供一个简单的示例?

谢谢

I am looking to start pulling data from comindwork, but I have never used any web services before so I don't really know how to get started.

Someone has suggested I can pull back data with either fopen or send a curl request, but I don't know which one.

I am using this api:

Projects
RETRIEVE
Get All Projects

GET /projects.xml
GET|POST /project/list 

Get One Project

GET /projects/{project_id}.xml
GET|POST /project/show/{project_id}
GET /projects/{project_id} 

Which one will do the job and could you provide a quick example please?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桜花祭 2024-12-01 02:01:35

关于fopen与curl:总是更喜欢curl,特别是当使用它超过1次时。
除了灵活性等显而易见的原因之外,curl 的速度仅快 5 倍左右。

regarding fopen vs curl: always prefer curl, especially when using it more than 1 time.
Obvious reasons like flexibility aside, curl is just about 5 times faster.

心碎的声音 2024-12-01 02:01:34

由于您需要能够 POST 和 GET,curl 将是一个不错的选择(尽管您可以轻松地使用两者 - fopen 用于 GET,curl 用于 POST)。

使用curl 的示例POST:

$ch = curl_init();
curl_setopt_array($ch, array(
  CURLOPT_POST => 1,
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_POSTFIELDS => http_build_query(array(
    'param' => 'value'
  )),
));
if(curl_exec($ch) === FALSE) {
  //handle failure
} else {
  //handle success
}
curl_close($ch);

这是一个基本示例,需要针对生产应用程序进行改进。例如,您可能需要使用curl_getinfo() 检查响应信息。

使用 fopen 的 GET 示例:

$fh = fopen($url, 'r');
$contents = fread($fh);
fclose($fh);

缺点是如果服务器返回有意义的标头,您将无法读取它们。最好使用 file_get_contents:

$content = file_get_contents($url);
print_r($http_response_header);

然后您就可以查阅 $http_response_header 变量来获取返回的标头。

Since you need to be able to POST as well as GET, curl would be a good choice (although you could easily use both - fopen for GET and curl for POST).

example POST using curl:

$ch = curl_init();
curl_setopt_array($ch, array(
  CURLOPT_POST => 1,
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_POSTFIELDS => http_build_query(array(
    'param' => 'value'
  )),
));
if(curl_exec($ch) === FALSE) {
  //handle failure
} else {
  //handle success
}
curl_close($ch);

This is a basic example which would need improvement for a production app. For example, you'd probably need to check response information using curl_getinfo().

example GET using fopen:

$fh = fopen($url, 'r');
$contents = fread($fh);
fclose($fh);

A downside is that if the server returns meaningful headers, you can't read them. Better to use file_get_contents:

$content = file_get_contents($url);
print_r($http_response_header);

You'd then be able to consult the $http_response_header variable to get the returned headers.

一紙繁鸢 2024-12-01 02:01:34

我会看看 http://www.phpclasses.org/package/3329-PHP-HTTP-client-using-the-PHP-Curl-library.html这是一个简单的、注释良好的curl包装器,可以为您提供如何的想法它有效=)

I'd have a look at http://www.phpclasses.org/package/3329-PHP-HTTP-client-using-the-PHP-Curl-library.html which is a simple, well commented wrapper around curl that could give you an idea of how it works =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文