Is the phppost request different from the post request in jquery? What should I do?

Source: Internet
Author: User
Is the phppost request different from the post request in jquery? I wrote a php sockethttp request, PHPcodepublicfunctionsendpost ($ postType = 'post') {$ url = $ this-php POST request is different from the post request in jquery?
Write a php socket http request,

PHP code
  Public function sendpost ($ postType = 'post') {$ url = $ this-> postURL; $ data = $ this-> postData = ''? Array (): $ this-> postData; // first parse the url $ url = parse_url ($ url); $ url_port =! Isset ($ url ['port'])? ($ Url ['scheme '] = 'http ')? 80: 443): $ url ['port']; if (! $ Url) {$ this-> errMessage = "couldn't parse url"; return false ;} // Construct the parameter in the format of URL key1 = value1 & key2 = value2 $ encoded = ""; while (list ($ k, $ v) = each ($ data )) {$ encoded. = ($ encoded? '&': ''); $ Encoded. = rawurlencode ($ k ). "= ". rawurlencode ($ v);} // echo $ encoded; exit; $ len = strlen ($ encoded); // splice the http header $ out = "{$ postType }". $ url ['path']. (isset ($ url ['query'])? ('? '. $ Url ['query']): ''). "HTTP/1.1 \ r \ n"; $ out. = "Host :". $ url ['host']. "\ r \ n"; $ out. = "Content-type: application/x-www-form-urlencoded \ r \ n"; $ out. = "Connection: Close \ r \ n"; $ out. = "Content-Length: $ len \ r \ n"; $ out. = "\ r \ n"; $ out. = $ encoded. "\ r \ n"; // open a sock $ fp = @ fsockopen ($ url ['host'], $ url_port); $ line = ""; if (! $ Fp) {$ this-> errMessage = "$ errstr ($ errno) \ n"; return false;} else {fwrite ($ fp, $ out); while (! Feof ($ fp) {$ line. = fgets ($ fp, 2048) ;}// remove the header file if ($ line) {$ body = stristr ($ line, "\ r \ n"); $ body = substr ($ body, 4, strlen ($ body); $ line = $ body ;} fclose ($ fp); return $ line ;}


At the same time, use jquery's ajax request for testing.

JScript code
  $.ajax({                    type : "POST",                    cache : false,                    url : _url,                    data : _data,                    dataType : 'json',                    success : function(data) {                                            }                });


It is found that php requests always return error information, while ajax requests return correct information, which should be the data returned by http post requests. there is no difference.
Can the server still detect ajax or php requests?

------ Solution --------------------
PHP code
[User: root Time: 03: 17: 09 Path:/home/apache/web] $ cat test. php
  "Baidu", 'B' => "client backend"); echo $ response;?> [User: root Time: 03: 17: 12 Path:/home/apache/web] $ cat main. php
  
------ Solution --------------------
In addition, https your code is powerless. you must use the context parameter supported by php to use stream_context_create with file_get_contents or fopen, or use curl directly.
Related Article

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.