這篇文章主要為大家展示了“php curl如何發(fā)送get或者post請求”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“php curl如何發(fā)送get或者post請求”這篇文章吧。
成都創(chuàng)新互聯(lián)長期為近千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新野企業(yè)提供專業(yè)的成都網(wǎng)站設計、網(wǎng)站制作,新野網(wǎng)站改版等技術服務。擁有十載豐富建站經驗和眾多成功案例,為您定制開發(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. 高級的post,可設置多個參數(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 會返回服務器響應代碼, // false 時只返回成功與否; $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 ''; 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
\n"; print_r($_POST);print "\n";以上是“php curl如何發(fā)送get或者post請求”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞標題:phpcurl如何發(fā)送get或者post請求
本文來源:http://weahome.cn/article/jjipgp.html