开发者

ajax to php to curl and back

I am trying to make an ajax call to a php script. The php script calls an rss feed using curl, gets the data, and returns the data to the funciton.

开发者_开发技巧

I keep getting an error

"Warning: Wrong parameter count for curl_error() in" ....

Here is my php code:1

$ch = curl_init() or die(curl_error());
        curl_setopt($ch, CURLOPT_URL, $feed);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data1 = curl_exec($ch) or die(curl_error());
        echo $data1;

and the ajax call:

$.ajax({ 
                       url: "getSingleFeed.php", 
                       type: "POST",
                       data: "feedURL=" + window.feedURL,
                       success: function(feed){

                    alert(feed);

                    }});

I tested all the variables, they are being passed correctly, I can echo them out. But this line: $data1 = curl_exec($ch) or die(curl_error());

is what is giving me the error. I am doing the same thing with curl on other pages, just without ajax, and it is working fine. Is there anything special I need to do with ajax to do this?


The PHP manual states that the first parameter of curl_error is a handle returned by curl_init();. Try replacing what you pasted with what is given below. This adds the $ch variable as a parameter to all the instances of curl_error();.

$ch = curl_init() or die(curl_error($ch));
        curl_setopt($ch, CURLOPT_URL, $feed);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data1 = curl_exec($ch) or die(curl_error($ch));
        echo $data1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜