如何使与PHP网页的请求? [英] How to make a request to a web page with PHP?

查看:126
本文介绍了如何使与PHP网页的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是:

一号通我要查询一个像谷歌的页面,但不填充它的搜索手动提交
2阶我想要得到的结果,并将其保存到数据库

1st- I want to query a page like google but without filling it's search filed manually 2nd- I want to get the result and save it to a database

我在这里看到用C#这样的一个例子。

I saw an example of doing this with C# here

<一个href=\"http://www.farooqazam.net/c-sharp-auto-click-button-and-auto-fill-form/comment-page-1/#comment-27256\" rel=\"nofollow\">http://www.farooqazam.net/c-sharp-auto-click-button-and-auto-fill-form/comment-page-1/#comment-27256

但我想用PHP做,你能帮助我吗?

but i'd like to do it with php, can you help me please?

感谢

推荐答案

您应该使用的卷曲这样做,不仅是因为它是<一个href=\"http://stackoverflow.com/questions/555523/file-get-contents-vs-curl-what-has-better-performance\">way比快的file_get_contents ,而且还因为它有更多的功能。使用它的另一个原因是,随着Xeoncross在评论中提到正确,可能的file_get_contents可以通过你的webhost出于安全原因关闭。

You should use cURL to do so, not only because it is way faster than file_get_contents, but also because it has many more features. Another reason to use it is that, as Xeoncross correctly mentioned in the comments, file_get_contents may be disabled by your webhost for security reasons.

有一个基本的例子是这样的:

A basic example would be this one:

$curl_handle = curl_init();
curl_setopt( $curl_handle, CURLOPT_URL, 'http://example.com' );
curl_exec( $curl_handle ); // Execute the request
curl_close( $curl_handle );

如果你需要从请求返回的数据,您需要指定 CURLOPT_RETURNTRANSFER 选项:

If you need the return data from the request, you need to specify the CURLOPT_RETURNTRANSFER option:

$curl_handle = curl_init();
curl_setopt( $curl_handle, CURLOPT_URL, 'http://example.com' );
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true ); // Fetch the contents too
$html = curl_exec( $curl_handle ); // Execute the request
curl_close( $curl_handle );

有万吨卷曲选项,例如,你可以设置一个请求超时:

There are tons of cURL options, for example, you can set a request timeout:

curl_setopt( $curl_handle, CURLOPT_CONNECTTIMEOUT, 2 ); // 2 second timeout

有关的所有选项的参考看到 curl_setopt()引用。

For a reference of all options see the curl_setopt() reference.

这篇关于如何使与PHP网页的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆