【问题标题】:curl with post method使用 post 方法卷曲
【发布时间】:2012-09-12 12:01:38
【问题描述】:

萨拉姆

我想从服务器获取一些 xml 响应

但是这个服务器只接受 xml 请求

我有这个代码示例

  <?php

$url = "http://www.pj.ma/phonexmlfeeds";

$post_string = '<?xml version="1.0 encoding="UTF-8" ?><search_requests> <search_request id="req1"><content>statut=1 AND linguistic_expansion2(lingex_qui_quoi,1,0,"qui_quoi","Restaurant",LIN_EXACT|LIN_LEM0|LIN_LEM1|LIN_LEM2|LIN_SYN1|LIN_SYN2|LIN_PHO1|LIN_ORT)</content><code>sortBy(casanet_perfect(lingex_qui_quoi));filterBy(casanet_perfect_filter(lingex_qui_quoi));setSortAttribute(indice,sortDirection,sortDirectionDesc);sortBy(indice,score2(lingex_qui_quoi,lingex_ou,matrice), ordered(lingex_qui_quoi),ordered(lingex_ou),rs);catalogBy(libniv3);sendxref(rs);sendxref(crs);sendxref(nomcomm);sendxref(activite);sendxref(libniv3);sendxref(villep);sendxref(adrp);sendxref(nomvoie);sendxref(numtel_typetel);sendxref(internet);sendxref(pub);sendxref(marque);sendXrefHighlights();</code><options><option name="maxFiles">10</option><option name="startFiles">11</option><option name="highlight_zone_start">{match}</option><option name="highlight_zone_end">{/match}</option></options><metadata><meta name="base">Pages Jaunes</meta></metadata></search_request></search_requests>';


$header  = "POST /phonexmlfeeds HTTP/1.1";
$header  = "Connection: close";
$header .= "Content-Type: text/xml";
$header .= "User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.3; GT-S5830 Build/GINGERBREAD)";
$header .= "Host: www.pj.ma"; 
$header .= "Content-Length: 1053";
$header .= "Accept-Encoding: gzip";
$header .= $post_string;

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);

$data = curl_exec($ch); 

if(curl_errno($ch))
    print curl_error($ch);
else
    curl_close($ch);

echo $data;

?>

但它给出了这个响应

您的浏览器发送了此服务器无法理解的请求。

这里我发布一个 wifisnifer 捕获的请求示例

POST /phonexmlfeeds HTTP/1.1 连接:关闭 内容类型:text/xml 用户代理:Dalvik/1.4.0(Linux;U;Android 2.3.3;GT-S5830 构建/姜饼)主机:www.pj.ma 内容长度:1052 接受编码:gzip

<?xml version="1.0 encoding="UTF-8" ?><search_requests> <search_request id="req1"><content>statut=1 AND linguistic_expansion2(lingex_qui_quoi,1,0,"qui_quoi","Restaurant",LIN_EXACT|LIN_LEM0|LIN_LEM1|LIN_LEM2|LIN_SYN1|LIN_SYN2|LIN_PHO1|LIN_ORT)</content><code>sortBy(casanet_perfect(lingex_qui_quoi));filterBy(casanet_perfect_filter(lingex_qui_quoi));setSortAttribute(indice,sortDirection,sortDirectionDesc);sortBy(indice,score2(lingex_qui_quoi,lingex_ou,matrice), ordered(lingex_qui_quoi),ordered(lingex_ou),rs);catalogBy(libniv3);sendxref(rs);sendxref(crs);sendxref(nomcomm);sendxref(activite);sendxref(libniv3);sendxref(villep);sendxref(adrp);sendxref(nomvoie);sendxref(numtel_typetel);sendxref(internet);sendxref(pub);sendxref(marque);sendXrefHighlights();</code><options><option name="maxFiles">10</option><option name="startFiles">1</option><option name="highlight_zone_start">{match}</option><option name="highlight_zone_end">{/match}</option></options><metadata><meta name="base">Pages Jaunes</meta></metadata></search_request></search_requests>

【问题讨论】:

  • 为什么要使用自定义标题而不是curl_setopt

标签: php xml curl


【解决方案1】:

对于自定义标题添加换行符 (\r\n)。

或者考虑使用http://us3.php.net/curl_setopt作为标题,例如:

curl_setopt($ch, CURLOPT_HTTPHEADERS, array(
    'POST /phonexmlfeeds HTTP/1.1',
    'Connection: close',
    'Content-Type: text/xml',
    etc...
    ));

【讨论】:

    【解决方案2】:

    编辑这一行 --

    $header  .= "Connection: close";
    

    如你所愿——

    $header  = "Connection: close";
    

    【讨论】:

    • 我更正了第一行中缺少的 post_string 以及 de $header = "Connection: close" 中的 .;我收到此错误请求标头字段缺少 ':' 分隔符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2015-01-06
    • 2018-09-20
    • 2011-05-15
    • 2019-11-02
    • 2017-05-10
    • 2019-02-25
    相关资源
    最近更新 更多