我正在向Api做几个请求。当我在我的计算机上测试我的开发服务器时,一切都很顺利。直到现在我才将它部署到我的ubuntu服务器上,它不再工作了。在做帖子时我收到400错误。
这是我的cURL代码:
include('curl/curl.php');
include('curl/curl_response.php');
$this->curl = new \Curl();
$this->curl->follow_redirects = false;
$this->curl->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
$this->curl->options["CURLOPT_SSL_VERIFYPEER"] = false;
$this->curl->options['AUTOREFERER'] = true;
$this->curl->headers = array(
"Content-Type"=>"application/json;charset=utf-8",
"X-API-Client-ID"=>"12D8"
);
//it's dutch. 'Gebruikersnaam' means 'username', 'Wachtwoord' means 'password' and 'IngelogBlijven' means 'StayLoggedIn'
$this->curl->post($loginUrl, json_encode(array('Gebruikersnaam' => $this->user, 'Wachtwoord' => $this->pass, "IngelogdBlijven" => true))))
我没有把所有这些都写成我自己但是从github得到了它,但它已经过时了所以修复了所有内容,直到它再次起作用,但是,它不能在我的服务器上工作...... :(
curl.php:
<?php
/**
* A basic CURL wrapper
*
* See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
*
* @package curl
* @author Sean Huber <shuber@huberry.com>
**/
class Curl {
/**
* The file to read and write cookies to for requests
*
* @var string
**/
public $cookie_file;
/**
* Determines whether or not requests should follow redirects
*
* @var boolean
**/
public $follow_redirects = true;
/**
* An associative array of headers to send along with requests
*
* @var array
**/
public $headers = array();
/**
* An associative array of CURLOPT options to send along with requests
*
* @var array
**/
public $options = array();
/**
* The referer header to send along with requests
*
* @var string
**/
public $referer;
/**
* The user agent to send along with requests
*
* @var string
**/
public $user_agent;
/**
* Stores an error string for the last request if one occurred
*
* @var string
* @access protected
**/
protected $error = '';
/**
* Stores resource handle for the current CURL request
*
* @var resource
* @access protected
**/
protected $request;
/**
* Initializes a Curl object
*
* Sets the $cookie_file to "curl_cookie.txt" in the current directory
* Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)' otherwise
**/
function __construct() {
$this->cookie_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'curl_cookie.txt';
$this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)';
}
/**
* Makes an HTTP DELETE request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse object
**/
function delete($url, $vars = array()) {
return $this->request('DELETE', $url, $vars);
}
/**
* Returns the error string of the current request if one occurred
*
* @return string
**/
function error() {
return $this->error;
}
/**
* Makes an HTTP GET request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse
**/
function get($url, $vars = array()) {
if (!empty($vars)) {
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
}
return $this->request('GET', $url);
}
/**
* Makes an HTTP HEAD request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse
**/
function head($url, $vars = array()) {
return $this->request('HEAD', $url, $vars);
}
/**
* Makes an HTTP POST request to the specified $url with an optional array or string of $vars
*
* @param string $url
* @param array|string $vars
* @return CurlResponse|boolean
**/
function post($url, $vars = array()) {
return $this->request('POST', $url, $vars);
}
/**
* Makes an HTTP PUT request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse|boolean
**/
function put($url, $vars = array()) {
return $this->request('PUT', $url, $vars);
}
/**
* Makes an HTTP request of the specified $method to a $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $method
* @param string $url
* @param array|string $vars
* @return CurlResponse|boolean
**/
function request($method, $url, $vars = array()) {
$this->error = '';
$this->request = curl_init();
if (is_array($vars)) $vars = http_build_query($vars, '', '&');
$this->set_request_method($method);
$this->set_request_options($url, $vars);
$this->set_request_headers();
$response = curl_exec($this->request);
if ($response) {
$response = new CurlResponse($response);
} else {
$this->error = curl_errno($this->request).' - '.curl_error($this->request);
}
curl_close($this->request);
return $response;
}
/**
* Formats and adds custom headers to the current request
*
* @return void
* @access protected
**/
protected function set_request_headers() {
$headers = array();
foreach ($this->headers as $key => $value) {
$headers[] = $key.': '.$value;
}
curl_setopt($this->request, CURLOPT_HTTPHEADER, $headers);
}
/**
* Set the associated CURL options for a request method
*
* @param string $method
* @return void
* @access protected
**/
protected function set_request_method($method) {
switch (strtoupper($method)) {
case 'HEAD':
curl_setopt($this->request, CURLOPT_NOBODY, true);
break;
case 'GET':
curl_setopt($this->request, CURLOPT_HTTPGET, true);
break;
case 'POST':
curl_setopt($this->request, CURLOPT_POST, true);
break;
default:
curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method);
}
}
/**
* Sets the CURLOPT options for the current request
*
* @param string $url
* @param string $vars
* @return void
* @access protected
**/
protected function set_request_options($url, $vars) {
curl_setopt($this->request, CURLOPT_URL, $url);
if (!empty($vars)) curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars);
# Set some default CURL options
curl_setopt($this->request, CURLOPT_HEADER, true);
curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent);
if ($this->cookie_file) {
curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
}
if ($this->follow_redirects) curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
if ($this->referer) curl_setopt($this->request, CURLOPT_REFERER, $this->referer);
# Set any custom CURL options
foreach ($this->options as $option => $value) {
curl_setopt($this->request, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
}
}
}
curl_response:
<?php
/**
* Parses the response from a Curl request into an object containing
* the response body and an associative array of headers
*
* @package curl
* @author Sean Huber <shuber@huberry.com>
**/
class CurlResponse {
/**
* The body of the response without the headers block
*
* @var string
**/
public $body = '';
/**
* An associative array containing the response's headers
*
* @var array
**/
public $headers = array();
/**
* Accepts the result of a curl request as a string
*
* <code>
* $response = new CurlResponse(curl_exec($curl_handle));
* echo $response->body;
* echo $response->headers['Status'];
* </code>
*
* @param string $response
**/
function __construct($response) {
# Headers regex
$pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
# Extract headers from response
preg_match_all($pattern, $response, $matches);
$headers_string = array_pop($matches[0]);
$headers = explode("\r\n", str_replace("\r\n\r\n", '', $headers_string));
# Remove headers from the response body
$this->body = str_replace($headers_string, '', $response);
# Extract the version and status from the first header
$version_and_status = array_shift($headers);
preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
$this->headers['Http-Version'] = $matches[1];
$this->headers['Status-Code'] = $matches[2];
$this->headers['Status'] = $matches[2].' '.$matches[3];
# Convert headers into an associative array
foreach ($headers as $header) {
preg_match('#(.*?)\:\s(.*)#', $header, $matches);
$this->headers[$matches[1]] = $matches[2];
}
}
/**
* Returns the response body
*
* <code>
* $curl = new Curl;
* $response = $curl->get('google.com');
* echo $response; # => echo $response->body;
* </code>
*
* @return string
**/
function __toString() {
return $this->body;
}
}
所以我的问题是:
如何解决此问题,以免我的服务器上出现400错误
答案 0 :(得分:0)
知道了! 这只是一个愚蠢的权限问题......