[英]PHP curl post call is not working, rather it's redirecting to login page
嘗試使用curl函數從php客戶端調用Java服務。 我的api期望像下面這樣的json請求正文,
{"personRequest":{"emailId":"rupanjan@gmail.com","isActive":"true"}}
和我的PHP客戶端代碼如下
$postData['personRequest'] = array(
'emailId' => $emailId,
'isActive' => "true"
);
//$data = Array();
//$data['personRequest']['isActive'] = "true";
//$postFields = json_encode($data);
$postFields = json_encode($postData);
$url = "$server_url" . "service/membership/update";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($postFields))
);
$result = curl_exec($ch);
var_dump(curl_getinfo($ch));
curl_close($ch);
echo $ch;
print_r($result);
執行后,它顯示為空響應,我也可以訪問服務器代碼,試圖在我的Java代碼中放入調試點,似乎http調用根本沒有到達我的服務器。
剛剛執行了var_dump(curl_getinfo($ ch)); 得到下面的輸出,array(22){[“” url“] => string(66)” http://9splatform.com/sample-server/service/membership/update “ [” content_type“] => NULL [” http_code “] => int(302)[” header_size“] => int(306)[” request_size“] => int(215)[” filetime“] => int(-1)[” ssl_verify_result“] => int (0)[“ redirect_count”] => int(0)[“ total_time”] => float(0.004441)[“ namelookup_time”] => float(0.000245)[“ connect_time”] => float(0.000705)[“ pretransfer_time “] => float(0.000714)[” size_upload“] => float(65)[” size_download“] => float(0)[” speed_download“] => float(0)[” speed_upload“] => float( 14636)[“ download_content_length”] => float(0)[“ upload_content_length”] => float(65)[“ starttransfer_time”] => float(0.004389)[“ redirect_time”] => float(0)[“ certinfo” ] => array(0){} [“ redirect_url”] =>字符串(95)“ http://9splatform.com/sample-server/login.html;jsessionid=A32AF520FE766BB5D444823D1419F4M9 ”}
這意味着這正在重定向到登錄頁面,任何人都可以幫助我...我不知道它為什么發生...
您應該使用curl_setopt($ch, CURLOPT_POST, 1);
而不是curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
<?php
$postData['personRequest'] = array(
'emailId'=>"rupanjan@gmail.com",
'isActive' => "true"
);
$postFields = json_encode($postData['personRequest']); /JSON encoding the array
$url = "$server_url" . "service/membership/update";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Removed the header...
$result = curl_exec($ch);
curl_close($ch);
echo $result;
in LOGIN page
================
$url = $server_url . "login.html";
$postdata = http_build_query(array(
'username' => $username,
'password' => $password
));
$tmp_fname = tempnam("/tmp", "COOKIE");
$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_fname);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json')
);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
in SUBSEQUENT requests
===========================
I have to add the cookies for every protected resource call...
$id = $this->input->post('id');
$emailId = $this->input->post('emailId');
$password = $this->input->post('password');
$tmp_fname = $this->session->userdata('cookies_file');
$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
$personObj['personRequest'] = array(
'emailId' => $emailId,
'isActive' => "true"
);
$postdata = json_encode($personObj);
$url = $server_url . 'service/membership/update';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmp_fname);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json')
);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.