PHP CI - curl

摘要:PHP CI - curl

我想要在PHP CI 使用curl

有試過網路上的 curl library,不過試失敗。

剛好有找到一篇文章

http://www.silverphp.com/php-curl-function-for-post-and-get-request.html

 

這我有試過,可以成功,以下是post的程式。


function curlUsingPost($url, $data)
{
if(empty($url) OR empty($data))
{
return 'Error: invalid Url or Data';
}

//url-ify the data for the POST
$fields_string = '';
foreach($data as $key=>$value) {
$fields_string .= $key.'='.$value.'&'; 
}
$fields_string = rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); # timeout after 10 seconds, you can increase it
   //curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  # Set curl to return the data instead of printing it to the browser.
   curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); # Some server may refuse your request if you dont pass user agent

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}