doubo1883 2015-09-14 23:33
浏览 257
已采纳

使用cURL对tomcat localhost进行PHP请求并获取响应

I'm new to PHP and am currently struggling to set this up. Could someone provide me an outline of how you use curl to post and retrieve the returning data?

I tried this out:

$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

But I keep getting the HTTP status 405 Error (aka this): Sadness

Am I doing something wrong/what should I do? Or do I have to change something in my ds/stuff

  • 写回答

1条回答 默认 最新

  • dtl85148 2015-09-14 23:36
    关注

    You should set CURLOPT_POST to true. POST data goes to CURLOPT_POSTFIELDS.

    $url = 'http://localhost:8080/ds/stuff?maybe=false';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?