网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

phpcurl如何发送get或者post请求

这篇文章主要为大家展示了“php curl如何发送get或者post请求”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“php curl如何发送get或者post请求”这篇文章吧。

成都创新互联长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为新野企业提供专业的成都网站设计、网站制作新野网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。

php可以扮演资料的发送者。

  1. 简单的get

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://SomeDomain/SamplePath?SomeVar=test");
curl_exec($ch);
curl_close($ch);

2.  简单的post

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://SomeDomain/SamplePath");
curl_setopt($ch, CURLOPT_POST, true); // 啟用POST
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( array( "a"=>"123", "b"=>"321") )); 
curl_exec($ch); 
curl_close($ch);

3. 高级的post,可设置多个参数

$toURL = "http://SomeDomain/SamplePath?SomeVar=XX";
$post = array(
  "a"=>"123",
  "b"=>"321",
);
$ch = curl_init();
$options = array(
  CURLOPT_URL=>$toURL,
  CURLOPT_HEADER=>0,
  CURLOPT_VERBOSE=>0,
  CURLOPT_RETURNTRANSFER=>true,
  CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)",
  CURLOPT_POST=>true,
  CURLOPT_POSTFIELDS=>http_build_query($post),
);
curl_setopt_array($ch, $options);
// CURLOPT_RETURNTRANSFER=true 会返回服务器响应代码,
// false 时只返回成功与否;
$result = curl_exec($ch); 
curl_close($ch);
echo $result;

4. post 发送文件

发送端代码:

$target_url = '/upload/otherpic65/110483.jpg');
        /* curl will accept an array here too.
         * Many examples I found showed a url-encoded string instead.
         * Take note that the 'key' in the array will be the key that shows up in the
         * $_FILES array of the accept script. and the at sign '@' is required before the
         * file name.
         */
	$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);         
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$target_url);
	curl_setopt($ch, CURLOPT_POST,1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	$result=curl_exec ($ch);
	curl_close ($ch);
	echo $result;	

接收端代码:

$uploaddir = realpath('./') . '/';$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);echo '
';
	if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
	    echo "File is valid, and was successfully uploaded.\n";
	} else {
	    echo "Possible file upload attack!\n";
	}
	echo 'Here is some more debugging info:';
	print_r($_FILES);
	echo "\n\n";
	print_r($_POST);print "\n";

以上是“php curl如何发送get或者post请求”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


新闻标题:phpcurl如何发送get或者post请求
转载注明:http://bjjierui.cn/article/jjipgp.html

其他资讯