PHP Curl上的错误请求

时间:2014-02-10 05:14:12

标签: php curl

我在使用自定义标头的PHP curl命令时遇到问题。谁能看到这里发生了什么?

  $url = "https://api.sitescout.com/oauth/token";
  $ch = curl_init();

  $headers  = array(
    "POST https://api.sitescout.com/oauth/token HTTP/1.1",
    "Host: api.sitescout.com",
    "Authorization: Basic bGFtcGhvdXNlLWFwaTp2RmhsSmlJMm5BT****",
    "Content-Type: application/x-www-form-urlencoded",
    "Accept: application/json",
    "Content-Length: 41"
  );

  $post_fields = array(
    'grant_type' => 'client_credentials'
  );

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

  $output = curl_exec($ch);
  $info = curl_getinfo($ch);

print_r($output);

  curl_close($ch);

  //var_dump($output);

这是服务器响应。

HTTP / 1.1 400错误请求 服务器:Apache-Coyote / 1.1 日期:星期一,2014年2月10日05:13:09 GMT 连接:关闭

非常感谢任何帮助!!

2 个答案:

答案 0 :(得分:0)

我认为您还需要添加USERAGENT参数:

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');

这意味着什么类型的代理正在请求内容。

答案 1 :(得分:0)

你可以这样试试,

$url = "https://api.sitescout.com/oauth/token";
$requestType ='POST';
$accessToken = 'Insert Your accessToken';

/* data you want to post */
$data = array(
          'subject'=>$subject,
          'starttime'=>$start,
          'endtime'=>$end, 
          'passwordrequired'=>'false'
        );

$postData = json_encode ($data);
$headers = getJsonHeaders($accessToken);

makeApiRequest($url, $requestType, $postData, $headers);

public function makeApiRequest ($url = null, $requestType = 'GET', $postData = array(),$headers = array()) 
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if ($requestType == 'POST') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    }else if($requestType == 'PUT'){
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    }

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $data = curl_exec($ch);

    $validResponseCodes = array(200, 201);
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

    if($requestType == 'PUT'){
        return $responseCode;
    }

    if (curl_errno($ch)) {
        curl_close($ch);
        return curl_error($ch);
    }
    elseif (!in_array($responseCode, $validResponseCodes)) {
        if (isJson($data)) {
            $data = json_decode($data);
        }
    }

    curl_close($ch);
    return $data;
}

public function getJsonHeaders ($accessToken) 
{
    return array(
        "HTTP/1.1",
        "Content-type: application/json",
        "Accept: application/json",
        "Authorization: OAuth oauth_token=". $accessToken
    );
}

public function isJson ($string) 
{
    $isJson = 0;
    $decodedString = json_decode($string);

    if (is_array($decodedString) || is_object($decodedString)) {
        $isJson = 1;
    }

    return $isJson;
}