0

Here is my code, I have two cURL statements in the same program. The first one uses $ch and second uses $ch1. The problem is first one is getting executed and showing the output but second one does nothing.

<?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 but the second curl request is not working:

            // 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.

?>

I can make request to second curl request manually from my browser and it works but it isn't working here. What's wrong?

4
  • @Alrazah When I make rrquest from browser, it shows success message in json but in php it isnt getting executed with no success or error messages at all . Commented Oct 12, 2014 at 17:54
  • PLease reduce that code to what is relevant here. Thanks. Commented Oct 12, 2014 at 17:55
  • you have error_reporting activated in your php? error_reporting(E_ALL); ini_set('display_errors', '1'); Commented Oct 12, 2014 at 18:01
  • @Alrazah yes but still no error Commented Oct 12, 2014 at 18:05

2 Answers 2

1

I think you get this error when you dump curl_error($ch1) :

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

You can add this line when you curl https if you have no sensitive transiting data :

curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);

Here is the code which works for me :

$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));

When I do this, I get :

string(97) "{ "message": "Unknown API key", "detail": { "error": 2002, "key": "MY KEY HERE" } }" string(0) ""
Sign up to request clarification or add additional context in comments.

9 Comments

And what do you get when you dump curl_error($ch1) ? That's weird because when I add this line, it works for me with your site.
I did var_dump ($ch1) and it gave: bool(false)
on curl_error it gave unknown ssl error. i have added the curl option you said after SETOPT_URL
I edited my answer to show you where I put the SSL handling line. What's the exact error you get on curl_error ?
I just tried your code and that too gave me: bool(false) string(69) "Unknown SSL protocol error in connection to api.idolondemand.com:443 "
|
0

I have set VERIFYPEER to false and VERIFYHOST to 1 and it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.