guzzle 6 post方法不起作用

时间:2015-06-26 09:11:06

标签: php python django yii2 guzzle

对于api pyhton Django 1.5,我正在使用yii2和guzzle版本4 当我试图发布数据与邮递员工作,但当我尝试用PHP时说错误请求404

error is GuzzleHttp\Exception\ClientException
v1/jobs/ [status code] 400 [reason phrase] BAD REQUEST

发布man格式数据工作数据格式

url -vm.xxx.com/v1/jobs/
 {
      "job_position":"Test Postion",
      "eligibility_course_id":["1","2","3","4"],
      "eligibiltiy_course_ids":["1","2","3","4"],
      "eligibility_branch_id":["5","6","7","8"],
      "eligibiltiy_skill_ids":["10","23","24"],
      "eligibiltiy_sublocation_ids":["10","23","24"],
      "location":["26","30","2","3"],
      "job_category":["1","4","7"],
      "sms_category":"1",
      "posting_date":"31\/07\/2014",
      "expiry_date":"31\/08\/2014",
      "job_type_id":"1",
      "job_type_other":"",
      "hiring_process":["1"],
      "job_description":"Test Description<\/p>",
      "company_name":"Test Company name",
      "company_profile":"Test company profile<\/p>",
      "company_url":"",
      "seo_title":"Test Seo Title",
      "seo_description":"Test Seo Description",
      "seo_keyword":"Test seo Keyward",
      "response":"1","how_to_apply":"",
      "terms_agreement":"1",
      "specify_terms_agreement":"",
      "employer_id":"1008369",
      "button_submitForm":"Post Job",
      "posted_by_id":"114",
      "template_name":"job_description"
    }

使用php代码 方法-1

$arr=[
"job_position" => "Test Postion",
"eligibility_course_id" => ["1","2","3","4"],
"eligibiltiy_course_ids" => ["1","2","3","4"],
"eligibility_branch_id" => ["5","6","7","8"],
"eligibiltiy_skill_ids" => ["10","23","24"],
"eligibiltiy_sublocation_ids" => ["10","23","24"],
"location" => ["26","30","2","3"],
"job_category" => ["1","4","7"],
"sms_category" => "1",
"posting_date" => "31\/07\/2014",
"expiry_date" => "31\/08\/2014",
"job_type_id" => "1",
"job_type_other" => "",
"hiring_process" => ["1"],
"job_description" => "Test Description<\/p>",
"company_name" => "Test Company name",
"company_profile" => "Test company profile<\/p>",
"company_url" => "",
"seo_title" => "Test Seo Title",
"seo_description" => "Test Seo Description",
"seo_keyword" => "Test seo Keyward",
"response" => "1","how_to_apply" => "",
"terms_agreement" => "1",
"specify_terms_agreement" => "",
"employer_id" => "1008369",
"button_submitForm" => "Post Job",
"posted_by_id" => "114",
"template_name" => "job_description"
];
$client = new Client();
$apiurl=Yii::$app->params['apiurl'];
$url=$apiurl.'/v1/jobs/';
try {

       $response = $client->post($url, ['json' => $arr]);
}
catch (\GuzzleHttp\Exception\BadResponseException $e) {

    $req = $e->getRequest();
    $resp =$e->getResponse();
    echo "<pre>";
    var_dump($req);
    var_dump($resp);
    die('ss');
}

尝试方法-2

 $arr=json_encode($arr);
$request = $client->post($url,array(
    'content-type' => 'application/json'
),array());
$request->setBody($arr); #set body!
$response = $request->send();

然后我得到了

InvalidArgumentException
No method is configured to handle the content-type config key

1 个答案:

答案 0 :(得分:0)

这是我的错误,我传递了错误的约会日期

"posting_date" => "31\/07\/2014",
"expiry_date" => "31\/08\/2014",

而不是我传递

"posting_date" => "31/07/2014",
"expiry_date" => "31/08/2014",

然后正在运作