Created
February 27, 2014 06:14
-
-
Save hubsgz/9245284 to your computer and use it in GitHub Desktop.
php post json数据 and php 模拟post 提交普通数据
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* post json数据 | |
* | |
* @param str $url posturl | |
* @param str $data_string json字符串 | |
* | |
* @return json_string | |
*/ | |
function postjson($url, $data_string) | |
{ | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt( | |
$ch, | |
CURLOPT_HTTPHEADER, | |
array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string) | |
) | |
); | |
$result = curl_exec($ch); | |
return $result; | |
} | |
/** | |
* php 模拟post 提交数据 | |
* | |
* @param str $url posturl | |
* @param str $data_string 参数 | |
* | |
* @return response | |
*/ | |
function http_post($url, $data_string) | |
{ | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt( | |
$ch, | |
CURLOPT_HTTPHEADER, | |
array( | |
'Content-Type: application/x-www-form-urlencoded', | |
'Content-Length: ' . strlen($data_string) | |
) | |
); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment