首页 > 编程知识 正文

php发送http的简单介绍

时间:2023-12-27 22:26:16 阅读:323985 作者:GUUG

本文目录一览:

php如何发送带中文的http请求?

直接发送就好了,对于http请求分为get和post都是支持中文的,已变量的方式发送就行,服务器会自动进行编码的,不需要多做什么处理。

php怎么响应客户端发送http请求

使用$_POST['参数名']处理post方法提交的参数,$_GET['参数名']处理get方法参数.

eg:

如果url 为: index.html?name=123pwd=123

?php

$name = $_GET['name'];

$pwd = $_GET['pwd'];

do something;

?

如果url 为: index.html

name=123pwd=123

?php

$name = $_POST['name'];

$pwd = $_POST['pwd'];

do something;

?

如果只是处理如何要跳转到其他页面,可以用header("Location: 文件名");

如果是网页和php混合,在需要使用?php php语句;?处理就行;使用echo可以输出一些值到网页中.

PHP中如何发送HTTP请求

看起来你的代码正确,不知道你有什么问题。

这个方法不错,但是最好用一个封装好的类。

比如http_client之类的,网上这样的类挺多了,你可以搜索一下。

当然直接用socket也可以。

怎么用PHP发送HTTP请求

在网上搜素关键字  模拟表单提交  有GET方式的   POST方式的  都行这是一个例子

CURL

 

 $ch = curl_init();

 //组装用户名和密码

      //模拟提交两个数据   可以不提交

        $info['username'] = $this-username;//用户名

        $info['password'] = $this-pwd;//密码

        //模拟表单提交

        $params[CURLOPT_URL] = $this-url;    //请求url地址

        $params[CURLOPT_HEADER] = true; //是否返回响应头信息

        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回

        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向

        $params[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';//模拟浏览器

        $postfields = '';

//将表单要提交的数据编程URL拼接方式

        foreach ($info as $key = $value){

            $postfields .= urlencode($key) . '=' . urlencode($value) . '';

        }

        $params[CURLOPT_POST] = true;//POST方式   

        $params[CURLOPT_POSTFIELDS] = $postfields;

        curl_setopt_array($ch, $params); //传入curl参数

        $content = curl_exec($ch); //执行

php怎么发送http请求并接收返回值

摘一段代码给你。请参考。

/**

* Curl 远程post请求

* @param type $get_url 请求url

* @param type $postdata 请求参数

* @return boolean

*/

function postCurlDatas($get_url, $postdata = '', $other_options = array()) {

$curl = curl_init(); // 启动一个CURL会话

curl_setopt($curl, CURLOPT_URL, $get_url); // 要访问的地址

// curl_setopt($curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent']);

curl_setopt($curl, CURLOPT_AUTOREFERER, 1);

curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求

curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false); // 禁用全局DNS缓存

curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); //此参数必须在上面的参数之后,切记

if (!empty($other_options['userpwd'])) {

curl_setopt($curl, CURLOPT_USERPWD, $other_options['userpwd']);

}

if (!empty($other_options['time_out'])) {

curl_setopt($curl, CURLOPT_TIMEOUT, $other_options['time_out']);

} else {

curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环

}

curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回

$ret = curl_exec($curl); // 执行操作

if ($ret === false) {

echo 'Curl error: ' . curl_error($curl);

curl_close($curl);

return false;

}

if ($other_options['return_detail'] == true) {

$detail = curl_getinfo($curl);

if (is_array($detail)) {

$detail['return_content'] = $ret;

}

$ret = $detail;

}

curl_close($curl); // 关闭CURL会话

return $ret;

}

php哪些方式发送http请求

第一种实现方式:实用socket编程,通常我们实用fsockopen这个函数来创建一个socket连接,用fputs来发送一个请求

第二种实现方式:实用php的curl扩展,我们使用curl_init()来初始化一个连接,然后设置一堆的curl_setopt()的东西来设置url,post的数据等等,最后我们使用curl_exec()来实现请求。

第三种方式就是: 实用file_get_contents函数,其实我们平时抓取一个网页可能只实用它的第一个参数,其实它的第三个参数就有数据了

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。