這篇文章主要為大家展示了“php curl如何發(fā)送get或者post請(qǐng)求”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“php curl如何發(fā)送get或者post請(qǐng)求”這篇文章吧。
成都創(chuàng)新互聯(lián)長期為近千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新野企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,新野網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
php可以扮演資料的發(fā)送者。
簡單的get
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://SomeDomain/SamplePath?SomeVar=test"); curl_exec($ch); curl_close($ch);
2. 簡單的post
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://SomeDomain/SamplePath"); curl_setopt($ch, CURLOPT_POST, true); // 啟用POST curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( array( "a"=>"123", "b"=>"321") )); curl_exec($ch); curl_close($ch);
3. 高級(jí)的post,可設(shè)置多個(gè)參數(shù)
$toURL = "http://SomeDomain/SamplePath?SomeVar=XX"; $post = array( "a"=>"123", "b"=>"321", ); $ch = curl_init(); $options = array( CURLOPT_URL=>$toURL, CURLOPT_HEADER=>0, CURLOPT_VERBOSE=>0, CURLOPT_RETURNTRANSFER=>true, CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)", CURLOPT_POST=>true, CURLOPT_POSTFIELDS=>http_build_query($post), ); curl_setopt_array($ch, $options); // CURLOPT_RETURNTRANSFER=true 會(huì)返回服務(wù)器響應(yīng)代碼, // false 時(shí)只返回成功與否; $result = curl_exec($ch); curl_close($ch); echo $result;
4. post 發(fā)送文件
發(fā)送端代碼:
$target_url = '/upload/otherpic65/110483.jpg'); /* curl will accept an array here too. * Many examples I found showed a url-encoded string instead. * Take note that the 'key' in the array will be the key that shows up in the * $_FILES array of the accept script. and the at sign '@' is required before the * file name. */ $post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$target_url); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result=curl_exec ($ch); curl_close ($ch); echo $result;
接收端代碼:
$uploaddir = realpath('./') . '/';$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);echo '<pre>'; if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); echo "\n<hr />\n"; print_r($_POST);print "</pr" . "e>\n";
以上是“php curl如何發(fā)送get或者post請(qǐng)求”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁標(biāo)題:phpcurl如何發(fā)送get或者post請(qǐng)求
文章源于:http://jinyejixie.com/article18/jjipgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)、電子商務(wù)、ChatGPT、自適應(yīng)網(wǎng)站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)