PHP cURL发布失败或没有数据返回我哪里出错了?

时间:2012-07-11 03:49:54

标签: php soap curl

继续我的位置,试着找出为什么我没有得到$result进入它生成的URL会给我一个基于get或post的有效JSON结果。所以我认为我的问题是我如何使用cURL。所以我需要有人接受这个。

        //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($params));
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
        //execute post
        $result = curl_exec($ch);           
                    if(!$result)
                    {
                        $error = curl_error($ch); echo $error; return false;
                    }
        //close connection
        curl_close($ch);

        //return json_decode($result);
                    echo $result;

以上编辑代码来自原始帖子

$error不报告任何内容。我更改了返回json ...以回显看是否有任何操作,并在屏幕上打印出“不允许的关键字符。”。

编辑2

$url = http://domain.com/search
$params = array('q'=>'search,term')

$params通过构建$fields_string

的foreach循环
$fields_string = '?';
foreach($params as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = substr($fields_string,0,-1);

fields_string最后看起来像?ll=37.2790669,-121.874722&range=10(对于我目前正在做的事情,我有1-12个可选参数可以传递,这就是我按照我的方式构建它的原因。

2 个答案:

答案 0 :(得分:1)

尝试添加错误检查以确保curl_exec正在成功执行,并在服务器上收回有意义的错误消息。

类似的东西:

    $ch = curl_init();
    //Make string url safe with urlencode
    curl_setopt($ch,CURLOPT_URL,urlencode($url));
    curl_setopt($ch,CURLOPT_POST,count($params));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    if(!$result) {
        $error = curl_error();
        //$error now contains the error thrown when curl_exec failed to execute
        //echo this to terminal or to an error box in the browser?
    }
    curl_close($ch);

    return json_decode($result);

如果您仍需要帮助,请在此处发布您产生的错误。 另外,这里是我利用的两个函数的手册页:

http://www.php.net/manual/en/function.curl-error.php

http://www.php.net/manual/en/function.curl-exec.php

http://php.net/manual/en/function.urlencode.php

-Cheers

答案 1 :(得分:1)

//set POST variables
$url = "http://example.com/edsym/registration.php"; // URL to calc.cgi
$fields = array(
'namee'=>urlencode($name),
'city'=>urlencode($city),
'phh'=>urlencode($ph),
'emaill'=>urlencode($email),
'msg1'=>urlencode("Message type")
                );
$fields_string=" ";
//url-ify the data for the POST
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);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

我正在使用curl发布这样及其工作