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.