本文目錄一覽:
- 1、php如何發送帶中文的http請求?
- 2、php怎麼響應客戶端發送http請求
- 3、PHP中如何發送HTTP請求
- 4、怎麼用PHP發送HTTP請求
- 5、php怎麼發送http請求並接收返回值
- 6、php哪些方式發送http請求
php如何發送帶中文的http請求?
直接發送就好了,對於http請求分為get和post都是支持中文的,已變數的方式發送就行,伺服器會自動進行編碼的,不需要多做什麼處理。
php怎麼響應客戶端發送http請求
使用$_POST[‘參數名’]處理post方法提交的參數,$_GET[‘參數名’]處理get方法參數.
eg:
如果url 為: index.html?name=123pwd=123
?php
$name = $_GET[‘name’];
$pwd = $_GET[‘pwd’];
do something;
?
如果url 為: index.html
name=123pwd=123
?php
$name = $_POST[‘name’];
$pwd = $_POST[‘pwd’];
do something;
?
如果只是處理如何要跳轉到其他頁面,可以用header(“Location: 文件名”);
如果是網頁和php混合,在需要使用?php php語句;?處理就行;使用echo可以輸出一些值到網頁中.
PHP中如何發送HTTP請求
看起來你的代碼正確,不知道你有什麼問題。
這個方法不錯,但是最好用一個封裝好的類。
比如http_client之類的,網上這樣的類挺多了,你可以搜索一下。
當然直接用socket也可以。
怎麼用PHP發送HTTP請求
在網上搜素關鍵字 模擬表單提交 有GET方式的 POST方式的 都行這是一個例子
CURL
$ch = curl_init();
//組裝用戶名和密碼
//模擬提交兩個數據 可以不提交
$info[‘username’] = $this-username;//用戶名
$info[‘password’] = $this-pwd;//密碼
//模擬表單提交
$params[CURLOPT_URL] = $this-url; //請求url地址
$params[CURLOPT_HEADER] = true; //是否返迴響應頭信息
$params[CURLOPT_RETURNTRANSFER] = true; //是否將結果返回
$params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
$params[CURLOPT_USERAGENT] = ‘Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1’;//模擬瀏覽器
$postfields = ”;
//將表單要提交的數據編程URL拼接方式
foreach ($info as $key = $value){
$postfields .= urlencode($key) . ‘=’ . urlencode($value) . ”;
}
$params[CURLOPT_POST] = true;//POST方式
$params[CURLOPT_POSTFIELDS] = $postfields;
curl_setopt_array($ch, $params); //傳入curl參數
$content = curl_exec($ch); //執行
php怎麼發送http請求並接收返回值
摘一段代碼給你。請參考。
/**
* Curl 遠程post請求
* @param type $get_url 請求url
* @param type $postdata 請求參數
* @return boolean
*/
function postCurlDatas($get_url, $postdata = ”, $other_options = array()) {
$curl = curl_init(); // 啟動一個CURL會話
curl_setopt($curl, CURLOPT_URL, $get_url); // 要訪問的地址
// curl_setopt($curl, CURLOPT_USERAGENT, $GLOBALS [‘user_agent’]);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_POST, true); // 發送一個常規的Post請求
curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false); // 禁用全局DNS緩存
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); //此參數必須在上面的參數之後,切記
if (!empty($other_options[‘userpwd’])) {
curl_setopt($curl, CURLOPT_USERPWD, $other_options[‘userpwd’]);
}
if (!empty($other_options[‘time_out’])) {
curl_setopt($curl, CURLOPT_TIMEOUT, $other_options[‘time_out’]);
} else {
curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 設置超時限制防止死循環
}
curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區域內容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回
$ret = curl_exec($curl); // 執行操作
if ($ret === false) {
echo ‘Curl error: ‘ . curl_error($curl);
curl_close($curl);
return false;
}
if ($other_options[‘return_detail’] == true) {
$detail = curl_getinfo($curl);
if (is_array($detail)) {
$detail[‘return_content’] = $ret;
}
$ret = $detail;
}
curl_close($curl); // 關閉CURL會話
return $ret;
}
php哪些方式發送http請求
第一種實現方式:實用socket編程,通常我們實用fsockopen這個函數來創建一個socket連接,用fputs來發送一個請求
第二種實現方式:實用php的curl擴展,我們使用curl_init()來初始化一個連接,然後設置一堆的curl_setopt()的東西來設置url,post的數據等等,最後我們使用curl_exec()來實現請求。
第三種方式就是: 實用file_get_contents函數,其實我們平時抓取一個網頁可能只實用它的第一個參數,其實它的第三個參數就有數據了
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/237083.html