doutan5844 2014-12-30 02:17
浏览 405

PHP curl_exec无法正常工作

I had a try to use the curl but the url are not executing causing the posting failure. The code are as shown.

$url = "www.something.com/somewhereelse.php";
$dataurl = "someparam";
$req = curl_init();
curl_setopt($req, CURLOPT_URL,$url);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_HEADER, false);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POSTFIELDS, "?DATA=$dataurl");
curl_exec($req);

Is there any thing wrong with my code???

  • 写回答

1条回答 默认 最新

  • douyue1998 2014-12-30 02:22
    关注

    First you need to add, at least,

    curl_setopt($req, CURLOPT_POST, true);
    

    and then you can do this, notice I removed the leading ? It is not needed.

    curl_setopt($req, CURLOPT_POSTFIELDS, "DATA=$dataurl");
    

    You should also be doing some error checking such as:

    if ($ret === false || $info['http_code'] != 200) {
       $ret = "No cURL data returned for $url [". $info['http_code']. "]";
       if (curl_error($req))
         $ret .= "
    ". curl_error($req);
    }
    curl_close($req);
    echo $ret;
    

    where $ret = curl_exec($req);

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号