开发者

Simple Curl request not working

I have a function make_curl_request to make curl request.

/**
 * General Function to make curl request */
function make_curl_request($url, $data)
{
    ob_start();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_exec($ch);
    curl_close($ch);
    $strCurlResponse = ob_get_contents();
    ob_end_clean();
    return $strCurlResponse;
}

I am calling it like:

$strGatewayResponse = make_curl_request( REQUEST_URL, compact('strMobileNo', 'strKeywords', 'strApiKey') );

I tried the things but can't get my code working fine. Currently its just return string("") as th开发者_运维问答e output. Where am i going wrong?

My target is to simple post few data to next page located on other domain and get its xml response and parse it and display it. Is there any other simple and good solution?


The problem is that you've got RETURNTRANSFER set to TRUE, which means curl returns its output instead of directly outputting it. However, you're not capturing that output in a variable, so it's dropping on the floor.

You've got two options

a) remove the ob_*() functions to remove the PHP buffering and then do

$data = curl_exec($ch)
if ($data === FALSE) {
    die("Curl failed: " . curL_error($ch));
}

after which $data contains the contents of the URL you've fetched.

b) remove the RETURNTRANSFER option, and let curl do its normal "output to client directly" thing, which then gets captured by the PHP output buffering.


Try by adding a row:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.


it's not ok to send a curl POST without a header. please find the following link. it may help OAuth, PHP, Rest API and curl gives 400 Bad Request

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜