参数获得“丢失”当使用cURL时 [英] Parameter gets "lost" when using cURL

查看:237
本文介绍了参数获得“丢失”当使用cURL时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个接口,其中我收到一大堆事件数据,准备另一个系统,并通过基于网络的API将其推入系统。



接收和处理数据工作正常,但推送到其他系统失败,当使用cURL。看起来像是从querystring的最后一个参数丢失了。



我怎么知道?接口响应并告诉我缺少必需的参数。



这是我如何构建querystring(缩短了一点) b

  $ URI ='http:// remote-interface-host / serviceurl? 

$ querystring。='city ='。urlencode(utf8_decode($ town));
$ querystring。='& street ='。urlencode(utf8_decode($ street));
$ querystring。='& location ='。urlencode(utf8_decode($ location));
$ querystring。='& start ='。$ start;
$ querystring。='& end ='。$ end;
$ querystring。='& text ='。urlencode(utf8_decode(trim($ description)));
$ querystring。='& title ='。urlencode(utf8_decode(trim($ title)));
$ querystring。='& website ='。urlencode(utf8_decode($ website));

$ textlanguage ='& languageid = 1522908220065994400';
$ querystring。= $ textlanguage;

$ fullcurl = $ URI。$ querystring;

$ data = docurlcall($ fullcurl);

docurlcall()简单

  (); 
curl_setopt_array($ curl,array(
CURLOPT_RETURNTRANSFER => 0,
CURLOPT_TIMEOUT => 10,
CURLOPT_URL => $ url,
CURLOPT_POST => 1,
CURLOPT_USERAGENT =>'my-interface'
));

$ result = json_decode(curl_exec($ curl));

curl_close($ curl);

return $ result;
}

来自远程接口的反馈 >

{success:false,message:缺少必需参数'languageid',}



据我所见,这应该是一个很好的使用cURL,所以我不知道为什么他删除最后一个参数。



任何想法可能错误或缺失?



注意:我遇到了cURL,因为外部接口的开发人员害怕数据可能变得太大(我删除图像和更多的信息,更好的可读性)GET请求,因此我必须使用cURL执行POST请求。

解决方案

要除去查询字符串生成并让PHP为你工作吗?

  $ parameters = array(
'city'=> urlencode(utf8_decode($ town)),
'street'=> urlencode(utf8_decode($ street)),
'location'=> urlencode )),
'start'=> $ start,
'end'=> $ end,
'text'=> urlencode(utf8_decode ,
'title'=> urlencode(utf8_decode(trim($ title))),
'website'=> urlencode(utf8_decode($ website)),
'languageid'=> '1522908220065994400'
);

$ data = docurlcall(
sprintf(
%s?%s,
http:// remote-interface-host / serviceurl,
http_build_query($ parameters)

);

这可以导致更简单的bug搜索(至少更干净的代码会导致更简单的错误catching:))。


I am working on an interface where I receive a whole bunch of event data, prepare it for another system and push it into the system via a web-based API.

Receiving and processing the data works fine, but pushing it into the other system fails, when using cURL for that. It seems like that last parameter from the querystring gets lost.

How do I know that? The interface responds and tells me that a mandatory parameter is missing. It seems to always be the last one.

This is how I build the querystring (shortened a bit)

$URI = 'http://remote-interface-host/serviceurl?';

$querystring .= 'city='.urlencode(utf8_decode($town));
$querystring .= '&street='.urlencode(utf8_decode($street));
$querystring .= '&location='.urlencode(utf8_decode($location));
$querystring .= '&start='.$start;
$querystring .= '&end='.$end;
$querystring .= '&text='.urlencode(utf8_decode(trim($description)));
$querystring .= '&title='.urlencode(utf8_decode(trim($title)));
$querystring .= '&website='.urlencode(utf8_decode($website));

$textlanguage = '&languageid=1522908220065994400';  
$querystring .= $textlanguage;

$fullcurl = $URI.$querystring;

$data = docurlcall($fullcurl);

docurlcall() is pretty simple, too

function docurlcall($url)
{
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 0,
        CURLOPT_TIMEOUT => 10,
        CURLOPT_URL => $url,
        CURLOPT_POST => 1,      
        CURLOPT_USERAGENT => 'my-interface'
    ));

    $result = json_decode(curl_exec($curl));

    curl_close($curl);

    return $result;
}

Feedback from the remote interface is

{ "success":false, "message":"missing required parameter 'languageid'", }

As far as I can see, this should be a fine use of cURL, so I don´t get why he drops the last parameter. But maybe that´s only a symptom of something different going wrong.

Any ideas what could be wrong or missing?

Note beside: I am stuck with cURL because the developers of the external interface fear that the data could get too big (I dropped images and more information for better readability) for a GET request, so I have to do a POST request with cURL.

解决方案

Wouldn't it be a nicer setup to get rid of the query string generation and let PHP do the work for you?

$parameters = array(
  'city'        => urlencode(utf8_decode($town)),
  'street'      => urlencode(utf8_decode($street)),
  'location'    => urlencode(utf8_decode($location)),
  'start'       => $start,
  'end'         => $end,
  'text'        => urlencode(utf8_decode(trim($description))),
  'title'       => urlencode(utf8_decode(trim($title))),
  'website'     => urlencode(utf8_decode($website)),
  'languageid'  => '1522908220065994400'
);

$data = docurlcall(
  sprintf(
    "%s?%s",
    "http://remote-interface-host/serviceurl",
    http_build_query($parameters)
  )
);

This can lead to a more simpler bug search (at least more clean code leads me to more simpler error catching :)).

这篇关于参数获得“丢失”当使用cURL时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆