php-curl不起作用

时间:2014-10-12 17:48:12

标签: php curl

这是我的代码,我在同一个程序中有两个cURL语句。第一个使用$ ch,第二个使用$ ch1。问题是第一个是执行并显示输出,但第二个没有做任何事情。

<?php
include ('DBconnect.php');

if (isset($_POST['submit'])) {
$verified = "1";
$error = array();
if (empty($_POST['name'])) {
    $error[] = 'I am sure you have a name!';
}
else {
    $name = $_POST['name'];
}

if (empty($_POST['phone'])) {
    $error[] = 'Please enter your phone number with country code';
}
else {
    $Phone = $_POST['phone'];
}

if (empty($_POST['Password'])) {
    $error[] = 'Please choose a password ';
}
else {
    $Password = $_POST['Password'];
}

if (empty($error)) //send to Database if there's no error '
{ // If everything's OK...

    // Make sure the phone number is available:

    $query_verify_phone = "SELECT * FROM members  WHERE Phone ='$Phone'";
    $result_verify_phone = mysqli_query($dbc, $query_verify_phone);
    if (!$result_verify_phone) { //if the Query Failed ,similar to if($result_verify_phone==false)
        echo ' Database Error Occured ';
    }

    if (mysqli_num_rows($result_verify_phone) == 0) { // IF no previous user is using this phone number.
        $query_insert_user = "INSERT INTO `members` ( `Name`, `Phone`, `Password`, `Verified`) VALUES ( '$name', '$Phone', '$Password', '$verified')";
        $result_insert_user = mysqli_query($dbc, $query_insert_user);
        if (!$result_insert_user) {
            echo 'Query Failed ';
        }

        if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.
            $customerToken = "TOKEN HERE";
            $clientTransactionId = rand(55555, 77777);
            $duration = "180";
            $countryCode = "91";
            $z2vToken = "TOKEN HERE";
            $postData = array(
                'customerToken' => $customerToken,
                'clientTransactionId' => $clientTransactionId,
                'callerid' => $Phone,
                'duration' => $duration,
                'countryCode' => $countryCode,
                'z2vToken' => $z2vToken,
            );

            // create post body

            $post_body = '';
            foreach($postData as $key => $value) {
                $post_body.= urlencode($key) . '=' . urlencode($value) . '&';
            }

            $post_body = rtrim($post_body, '&');

            // Initialize CURL data to send via POST to the API
            // FIRST ONE CURL REQUEST- WORKING

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://www.zipdial.com/z2v/startTransaction.action");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);

            // Execute CURL command and return into variable ch

            $string = curl_exec($ch);
            curl_close($ch);
            $json = json_decode($string);

            // now the json has been decoded
            // echo "Please do a missed call  on: ";
            // echo "<img src=' ".$json->img."'>";

            $pf = 'fl' . uniqid();
            $un = uniqid($pf);
            $fpl = 'img' . $un . '.png';
            file_put_contents($fpl, file_get_contents($json->img));

以上所有内容都很顺利,但第二个卷曲请求无效:

            // EVERYTHING ABOVE GOES FINE. BELOW IS SECOND REQUEST- NOT WORKING

            $url = "https://api.idolondemand.com/1/api/sync/ocrdocument/v1";
            $post = array(
                'apikey' => "MY KEY HERE",
                'url' => "http://site.ext/users/$fpl",
                'mode' => "document_photo"
            );
            $ch1 = curl_init();
            curl_setopt($ch1, CURLOPT_URL, $url);
            curl_setopt($ch1, CURLOPT_POST, 1);
            curl_setopt($ch1, CURLOPT_POSTFIELDS, $post);
            curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
            $ocr = curl_exec($ch1);
            $jsonocr = json_decode($ocr, true);
            $textblock = $jsonocr['text'][0];
            echo '<div class="success">Please give a missed call to ' . $textblock['text'] . ' from your registered phone number to activate account. </div>';
            curl_close($ch1);
        }
        else { // If it did not run OK.
            echo '<div class="errormsgbox">You could not be registered due to a system  error. We apologize for any inconvenience.</div>';
        }
    }
    else { // The phone number is not available.
        echo '<div class="errormsgbox" >That phone number has already been registered. </div>';
    }
}
else { //If the "error" array contains error msg , display them
    echo '<div class="errormsgbox"> <ol>';
    foreach($error as $key => $values) {
        echo ' <li>' . $values . '</li>';
    }

    echo '</ol></div>';
}

mysqli_close($dbc); //Close the DB Connection
} // End of the main Submit conditional.

?>

我可以从我的浏览器手动请求第二个curl请求,但是它可以工作,但它不能在这里工作。怎么了?

2 个答案:

答案 0 :(得分:1)

我认为转储curl_error($ch1)时会出现此错误:

Unknown SSL protocol error in connection to api.idolondemand.com

如果您没有敏感的过境数据,则可以在卷曲https时添加此行:

curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);

这是适用于我的代码:

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 1);
$ocr = curl_exec($ch1);
var_dump($ocr);
var_dump(curl_error($ch1));

当我这样做时,我得到:

string(97) "{ "message": "Unknown API key", "detail": { "error": 2002, "key": "MY KEY HERE" } }" string(0) ""

答案 1 :(得分:0)

我已将VERIFYPEER设置为false,将VERIFYHOST设置为1并且它可以正常工作。