A. php,curl模擬post請求,獲取不到數據
獲取不到數據,通過別的方式檢查下伺服器是否有數據返回。比如先用get測試。
B. php 怎麼POST獲取數據
方法1、最常見的方法是:$_POST['fieldname'];
說明:只能接收Content-Type: application/x-www-form-urlencoded提交的數據
解釋:也就是表單POST過來的數據
方法2、file_get_contents("php://input");
說明:
允許讀取 POST 的原始數據。
和 $HTTP_RAW_POST_DATA 比起來,它給內存帶來的壓力較小,並且不需要任何特殊的 php.ini 設置。
php://input 不能用於 enctype="multipart/form-data"。
解釋:
對於未指定 Content-Type 的POST數據,則可以使用file_get_contents(「php://input」);來獲取原始數據。
事實上,用PHP接收POST的任何數據都可以使用本方法。而不用考慮Content-Type,包括二進制文件流也可以。
所以用方法二是最保險的方法
方法3、$GLOBALS['HTTP_RAW_POST_DATA'];
說明:
總是產生 $HTTP_RAW_POST_DATA 變數包含有原始的 POST 數據。
此變數僅在碰到未識別 MIME 類型的數據時產生。
$HTTP_RAW_POST_DATA 對於 enctype="multipart/form-data" 表單數據不可用
如果post過來的數據不是PHP能夠識別的,可以用 $GLOBALS['HTTP_RAW_POST_DATA']來接收,
比如 text/xml 或者 soap 等等
解釋:
$GLOBALS['HTTP_RAW_POST_DATA']存放的是POST過來的原始數據。
$_POST或$_REQUEST存放的是 PHP以key=>value的形式格式化以後的數據。
但$GLOBALS['HTTP_RAW_POST_DATA']中是否保存POST過來的數據取決於centent-Type的設置,即POST數據時 必須顯式示指明Content-Type: application/x-www-form-urlencoded,POST的數據才會存放到 $GLOBALS['HTTP_RAW_POST_DATA']中
C. PHP CURL POST模擬用戶登錄。。謝謝了。。希望能給出具體的代碼,並簡要解釋下代碼
你可以看下是否有其它的http header沒有模擬,比如Referer和User-Agent是否都能模擬瀏覽器的值,一個完整的請求是類似於這樣的:
GET /home/pack/data/content?id=31,2399,13,30&asyn=1&t=0.03439752989200834&_req_seqid=0xa982225f0637c78a HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: http://www..com/
x-requested-with: XMLHttpRequest
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS123401; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; MS-RTC LM 8)
Host: www..com
Connection: Keep-Alive
Cookie: XCXXXXX
D. 如何向php伺服器發送數據為json的post請求
用curl
$ch = curl_init();
$header[] = "Content-type: text/xml";//定義content-type為xml
curl_setopt($ch, CURLOPT_URL, $url); //定義表單提交地址
curl_setopt($ch, CURLOPT_POST, 1); //定義提交類型 1:POST ;0:GET
curl_setopt($ch, CURLOPT_HEADER, 1); //定義是否顯示狀態頭 1:顯示 ; 0:不顯示
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定義請求類型
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//定義是否直接輸出返迴流
curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr); //定義提交的數據,這里是XML文件
$res = curl_exec($ch);
curl_close($ch);//關閉
E. php post提交xml報文 QQ:568299309
以下是提交xml的示例代碼:
<?php
$xml_data ='<AATAvailReq1>'.
'<Agency>'.
'<Iata>1234567890</Iata>'.
'<Agent>lgsoftwares</Agent>'.
'<Password>mypassword</Password>'.
'<Brand>phpmind.com</Brand>'.
'</Agency>'.
'<Passengers>'.
'<Alt AGE="" ID="1"></Alt>'.
'<Alt AGE="" ID="2"></Alt>'.
'</Passengers>'.
'<HotelAvailReq1>'.
'<DestCode>JHM</DestCode>'.
'<HotelCode>OGGSHE</HotelCode>'.
'<CheckInDate>101009</CheckInDate>'.
'<CheckOutDate>101509</CheckOutDate>'.
'<UseField>1</UseField>'.
'</HotelAvailReq1>'.
'</AATAvailReq1>';
$URL = "https://www.yourwebserver.com/path/";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
?>
F. PHP如何向JAVA介面webservice發送xml
function httpPostXml($url='',$xmlData=''){
$server = $url;
//首先檢測是否支持curl
if (!extension_loaded("curl")) {
trigger_error("對不起,請開啟curl功能模塊!", E_USER_ERROR);
return null;
}
//構造xml
$xmldata= $xmlData;
//初始一個curl會話
$curl = curl_init();
//設置url
curl_setopt($curl, CURLOPT_URL,$server);
//設置發送方式:post
curl_setopt($curl, CURLOPT_POST, true);
//設置發送數據
curl_setopt($curl, CURLOPT_POSTFIELDS, $xmldata);
//不輸出瀏覽器,返回service返回值
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//抓取URL並把它傳遞給瀏覽器
$return = curl_exec($curl);
//關閉cURL資源,並且釋放系統資源
if(curl_errno($curl)){
echo curl_error($curl);
}
var_mp($return);
$xml = simplexml_load_string($return);
$returnData = json_decode(json_encode($xml),TRUE);
return $returnData;
}
G. php獲取數據為什麼curl獲取不完整
因為,PHP CURL庫默認1024位元組的長度不等待數據的返回,所以你那段代碼需增加一項配置:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
給你一個更全面的封裝方法:
function req_curl($url, &$status = null, $options = array())
{
$res = '';
$options = array_merge(array(
'follow_local' => true,
'timeout' => 30,
'max_redirects' => 4,
'binary_transfer' => false,
'include_header' => false,
'no_body' => false,
'cookie_location' => dirname(__FILE__) . '/cookie',
'useragent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1',
'post' => array() ,
'referer' => null,
'ssl_verifypeer' => 0,
'ssl_verifyhost' => 0,
'headers' => array(
'Expect:'
) ,
'auth_name' => '',
'auth_pass' => '',
'session' => false
) , $options);
$options['url'] = $url;
$s = curl_init();
if (!$s) return false;
curl_setopt($s, CURLOPT_URL, $options['url']);
curl_setopt($s, CURLOPT_HTTPHEADER, $options['headers']);
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, $options['ssl_verifypeer']);
curl_setopt($s, CURLOPT_SSL_VERIFYHOST, $options['ssl_verifyhost']);
curl_setopt($s, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($s, CURLOPT_MAXREDIRS, $options['max_redirects']);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_FOLLOWLOCATION, $options['follow_local']);
curl_setopt($s, CURLOPT_COOKIEJAR, $options['cookie_location']);
curl_setopt($s, CURLOPT_COOKIEFILE, $options['cookie_location']);
if (!empty($options['auth_name']) && is_string($options['auth_name']))
{
curl_setopt($s, CURLOPT_USERPWD, $options['auth_name'] . ':' . $options['auth_pass']);
}
if (!empty($options['post']))
{
curl_setopt($s, CURLOPT_POST, true);
curl_setopt($s, CURLOPT_POSTFIELDS, $options['post']);
//curl_setopt($s, CURLOPT_POSTFIELDS, array('username' => 'aeon', 'password' => '111111'));
}
if ($options['include_header'])
{
curl_setopt($s, CURLOPT_HEADER, true);
}
if ($options['no_body'])
{
curl_setopt($s, CURLOPT_NOBODY, true);
}
if ($options['session'])
{
curl_setopt($s, CURLOPT_COOKIESESSION, true);
curl_setopt($s, CURLOPT_COOKIE, $options['session']);
}
curl_setopt($s, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($s, CURLOPT_REFERER, $options['referer']);
$res = curl_exec($s);
$status = curl_getinfo($s, CURLINFO_HTTP_CODE);
curl_close($s);
return $res;
}
H. 使用php curl 模擬post請求,自動附加了data參數
$post_data_string=http_build_query($post_data,'&');
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$get_session_url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$xmloutput=curl_exec($ch);
一般這樣寫 你自己對比下
I. PHP使用CURL請求https的微信統一下單介面時報錯,同樣的代碼我在另一台機器上運行是正常的
我也遇到了同樣的問題,只要是走微信,偶爾都會請求不到,原來是正常的。今天排查了一天,終於找到了原因所在。
centos原生用的NSS,而不是OpenSSL,curl調用NSS庫請求https時偶爾會出現請求不到的情況。
解決方案:
參考網址:網頁鏈接
按步驟完成後記得重啟 php-fpm和nginx