PHP CURL 到 CodeIgniter 控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4622331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP CURL to CodeIgniter controller
提问by tpae
here's my code:
这是我的代码:
<?php
$url = 'http://localhost:2304/index.php/testproj/files/add/';
$name = "test";
$fields = array(
'name'=>urlencode($name)
);
$fields_string = "";
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
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($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
var_dump($result);
//close connection
curl_close($ch);
?>
I am trying to send post data to a CodeIgniter controller. I decided to use CURL to do the job. however, it doesn't work, when I put "blah" in my controller, it doesn't return anything. When I access the URL directly, it shows "blah".
我正在尝试将发布数据发送到 CodeIgniter 控制器。我决定使用 CURL 来完成这项工作。但是,它不起作用,当我在控制器中放入“blah”时,它不会返回任何内容。当我直接访问 URL 时,它显示“blah”。
回答by Phil Sturgeon
You could use my Curl library:
你可以使用我的Curl 库:
$this->load->library('curl');
$result = $this->curl->simple_get('http://example.com/');
var_dump($result);
回答by Ish
Try adding this into options
尝试将其添加到选项中
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
Edit 1
编辑 1
add this
添加这个
curl_setopt($ch, CURLOPT_HEADER, 1);
// and post the result of $result