PHP simulated Post submit data method Rollup _php instance

Source: Internet
Author: User
Tags urlencode

Using PHP to simulate post values although not much is used in daily life, it is often used in some situations. The following cloud-Habitat community Small series for everyone to organize three kinds of PHP analog post pass the value of the method, file_get_contents, curl and socket.

The first type: file_get_contents to simulate post

<php

function File_get_contents_post ($url, $post) {
$options = array (
' http ' => Array (
' method ') => ' POST ',
' content ' => http_build_query ($post),)
;
$result = File_get_contents ($url, False, Stream_context_create ($options));
return $result;
}
$data = File_get_contents_post ("http://www.a.com/post/post.php", Array (' name ' => ' caiknife ', ' email ' => ') Caiknife#gmail.com '));
Var_dump ($data);

The second type: Curl analog post

<php
function Curl_post ($url, $post) {
$options = array (
curlopt_returntransfer =>true,
Curlopt_header =>false,
curlopt_post =>true,
curlopt_postfields => $post,
);
$ch = Curl_init ($url);
Curl_setopt_array ($ch, $options);
$result = curl_exec ($ch);
Curl_close ($ch);
return $result;
}
$data = Curl_post ("http://www.a.com/post/post.php", Array (' name ' => ' caiknife ', ' email ' => ' caiknife#gmail.com ') );
Var_dump ($data);

The third type: socket to simulate post

<php function Socket_post ($url, $post) {$urls = Parse_url ($url); if (!isset ($urls [' Port ']) {$urls [' Port ']=80;} $fp =
Fsockopen ($urls [' Host '], $urls [' Port '], $errno, $ERRSTR);
if (! $fp) {echo "$errno, $errstr"; exit ();} $post = Http_build_query ($post);
$length = strlen ($post); $header =<< 

The last of these three methods are the same, you can get the post of the value, but when using the socket, send header information must pay attention to the full header information, such as the content type and content length must have, Connection:close and post data to a blank line, and so on, and the content through the socket is included header information, to deal with to get the real content.

Next to everyone say PHP simulated post submission request, call interface

/**
* analog post for URL request
* @param string $url
* @param string $param
/function request_post ($url = ', $ param = ') {
if (empty ($url) | | | empty ($PARAM)) {return
false;
}
$POSTURL = $url;
$curlPost = $param;
$ch = Curl_init ()///Initialize Curl
curl_setopt ($ch, Curlopt_url, $POSTURL);//crawl The specified page
curl_setopt ($ch, Curlopt_ Header, 0)//Set Header
curl_setopt ($ch, Curlopt_returntransfer, 1),//required result string and output to screen
curl_setopt ($ch, Curlopt_post, 1);//post Submission Method
curl_setopt ($ch, Curlopt_postfields, $curlPost);
$data = curl_exec ($ch);//Run Curl
curl_close ($ch);
return $data;

This is the method,

The following is a specific invocation case.

function Testaction () {
$url = ' http://mobile.jschina.com.cn/jschina/register.php ';
$post _data[' appid '] = ' ten ';
$post _data[' appkey '] = ' cmbohpffxvr03nipkkqxaaa1vf5no4nq ';
$post _data[' member_name '] = ' zsjs123 ';
$post _data[' password '] = ' 123456 ';
$post _data[' email '] = ' zsjs123@126.com ';
$o = "";
foreach ($post _data as $k => $v) 
{ 
$o. = "$k =". UrlEncode ($v). "&";
}
$post _data = substr ($o, 0,-1);
$res = $this->request_post ($url, $post _data); 
Print_r ($res);

This submits the request and gets the result of the request. Generally returned results are in JSON format.

Here the post is spliced out.

can also be transformed into the following way.

/**
* analog post for URL request
* @param string $url
* @param array $post _data
/function request_post ($url = ' ', $post _data = Array ()) {
if (empty ($url) | | | empty ($post _data)) {return
false;
}
$o = "";
foreach ($post _data as $k => $v) 
{ 
$o. = "$k =". UrlEncode ($v). "&";
}
$post _data = substr ($o, 0,-1);
$POSTURL = $url;
$curlPost = $post _data;
$ch = Curl_init ()///Initialize Curl
curl_setopt ($ch, Curlopt_url, $POSTURL);//crawl The specified page
curl_setopt ($ch, Curlopt_ Header, 0)//Set Header
curl_setopt ($ch, Curlopt_returntransfer, 1),//required result string and output to screen
curl_setopt ($ch, Curlopt_post, 1);//post Submission Method
curl_setopt ($ch, Curlopt_postfields, $curlPost);
$data = curl_exec ($ch);//Run Curl
curl_close ($ch);
return $data;
}

The stitching is also encapsulated, so the call time is more concise.

 function testaction () {$url = ' http://mobile.jschina.com.cn/jschina/register.php '; $
post_data[' appid '] = ' 10 ';
$post _data[' appkey '] = ' cmbohpffxvr03nipkkqxaaa1vf5no4nq ';
$post _data[' member_name '] = ' zsjs124 ';
$post _data[' password '] = ' 123456 ';
$post _data[' email '] = ' zsjs124@126.com ';
$post _data = Array (); 
$res = $this->request_post ($url, $post _data);
Print_r ($res); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.