×

php如何发送http请求

关于php如何发送http请求的信息

admin admin 发表于2023-03-31 01:51:11 浏览61 评论0

抢沙发发表评论

本文目录一览:

怎么用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';//模拟浏览器-php如何发送http请求

        $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请求

我觉得你这个正确,一眼看不出错在哪里,如果是我我就不检查了,我下面粘贴一个我使用正常的函数,你直接调用函数就可以了,调用语句可以这样:

$A=trim(urlencode($_REQUEST['A']));

$B=trim(urlencode($_REQUEST['B']));

$params="A=$AB=$B";

list($body,$header)=http_request('','POST',$params);

如果你无需检查返回结果,那就这样也可以:

http_request('','POST',$params);

函数定义如下:

//执行HTTP请求

function http_request($url,$method='GET',$data='',$cookie='',$refer=''){

$header='';

$body='';

$newcookie='';

if (preg_match('/^http:\/\/(.*?)(\/.*)$/',$url,$reg)){$host=$reg[1]; $path=$reg[2];}

else {outs(1,"URL($url)格式非法!"); return;}

$http_host=$host;

if (preg_match('/^(.*):(\d+)$/', $host, $reg)) {$host=$reg[1]; $port=$reg[2];}

else $port=80;

$fp = fsockopen($host, $port, $errno, $errstr, 30);

if (!$fp) {

outs(1,"$errstr ($errno)\n");

} else {

fputs($fp, "$method $path HTTP/1.1\r\n");

fputs($fp, "Host: $http_host\r\n");

if ($refer!='') fputs($fp, "Referer: $refer\r\n");

if ($cookie!='') fputs($fp, "Cookie: $cookie\r\n");

fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");

fputs($fp, "Content-length: ".strlen($data)."\r\n");

fputs($fp, "Connection: close\r\n\r\n");

fputs($fp, $data . "\r\n\r\n");

$header_body=0;

$chunked_format=0;

$chunked_len=0;

while (!feof($fp)) {

$str=fgets($fp);

//$len=hexdec($str); if ($header_body==1) {echo "$str\t$len\n"; $str=fread($fp,$len);echo $str;}-php如何发送http请求

if ($header_body==1){

if ($chunked_format){

if ($chunked_len=0){

$chunked_len=hexdec($str);

if ($chunked_len==0) break;

else continue;

} else {

$chunked_len-=strlen($str);

if ($chunked_len=0) $str=trim($str);

//elseif ($chunked_len==0) fgets($fp);

}

}

$body.=$str;

}

else if ($str=="\r\n") $header_body=1;

else {

$header.=$str;

if ($str=="Transfer-Encoding: chunked\r\n") $chunked_format=1;

if (preg_match('|Set-Cookie: (\S+)=(\S+);|',$str,$reg)) $newcookie.=($newcookie==''?'':'; ').$reg[1].'='.$reg[2];-php如何发送http请求

}

}

fclose($fp);

}

$GLOBALS['TRAFFIC']+=414+strlen($url)+strlen($data)+strlen($header)+strlen($body);

if (preg_match('/^Location: (\S+)\r\n/m',$header,$reg)) {

if (substr($reg[1],0,1)!='/'){

$path=substr($path,0,strrpos($path,'/')+1);

$path.=$reg[1];

} else $path=$reg[1];

if ($newcookie) $cookie=$newcookie;

return http_request('http://'.$http_host.$path,'GET','',$cookie,$url);

}

return array($body, $header, $newcookie);

}

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请求分为get和post都是支持中文的,已变量的方式发送就行,服务器会自动进行编码的,不需要多做什么处理。

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;

}