Phpcurl simulated Q & A, always 400 errors. I want to simulate the Q & A of this website: Success. This is an ajax submission. after all js verification passes, I think about the php curl simulated answer, which is always 400 error.
I want to simulate this website's Q: http://zsjs.kaiwind.com/kf/html/index.html
He logs on to the above page and goes to the new page to answer questions. after all the answers are completed and confirmed, he is allowed to submit them.
It is submitted by ajax. after all js verification is passed, recall that ajax requests are sent in the background to complete the page.
The data he sent is not complex, but errors are always made when simulating the data sent. the following code and error prompts are posted. hope you can save it.
Code:
Curl request section:
Function vget ($ url, $ header = null) {// simulate the function for retrieving content
Global $ browsers;
$ Curl = curl_init (); // Start a CURL session
Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed
Curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ browsers); // sets the HTTP header
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']); // simulate the browser used by the user
Curl_setopt ($ curl, CURLOPT_REFERER, 'http: // www.baidu.com ');
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
Curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular GET request
Curl_setopt ($ curl, CURLOPT_COOKIEJAR, $ GLOBALS ['cookie _ file']); // Save the cookie file
If (isset ($ header )&&! Is_null ($ header )){
Curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header); // Set the header information
}
Curl_setopt ($ curl, CURLOPT_HEADER, true );
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // sets the timeout limit to prevent endless loops.
Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.
$ TmpInfo = curl_exec ($ curl); // perform the operation
If (curl_errno ($ curl )){
Echo 'errno'. curl_error ($ curl );
}
Curl_close ($ curl); // Close the CURL session
Return $ tmpInfo; // return data
}
Function vpost ($ url, $ data) {// simulate the data submission function
Global $ browsers;
// $ Header = "Content-type: application/x-www-form-urlencoded"; // Define content-type as xml
$ Curl = curl_init (); // Start a CURL session
Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
Curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ browsers); // sets the HTTP header
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']); // simulate the browser used by the user
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
Curl_setopt ($ curl, CURLOPT_REFERER, 'http: // zsjs.kaiwind.com/kf/html/question.html? 15238132836? 10 ');
// Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
Curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); // data packet submitted by Post
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ GLOBALS ['cookie _ file']); // read the cookie information stored above
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // sets the timeout limit to prevent endless loops.
Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.
$ TmpInfo = curl_exec ($ curl); // perform the operation
If (curl_errno ($ curl )){
Echo 'errno'. curl_error ($ curl );
}
Curl_close ($ curl); // key CURL session
Return $ tmpInfo; // return data
}
Execution part:
Header ('content-type: text/html; charset = utf-8 ');
$ Phone = '000000 ';
$ Area = '10 ';
$ Cookie_jar = tempnam ('./', 'cooke'); // specify the location of the cookie file for saving sessionid
$ Data = array (
'Tel' => urlencode ($ phone ),
'Region' => urlencode ($ area)
);
$ GLOBALS ['cookie _ file'] = $ cookie_jar;
Require_once ('./functions. php ');
$ Url = 'http: // zsjs.kaiwind.com/kf/html/index.html ';
// Vget ($ url );