Curl Bad Request

We are getting error while using adding curl request in loop as shown in below code.
eventhough first entry from the loop gets consumed properly , from 2nd loop onwards we get error..

#$data is large XML file, which we divide in chunks and pass in for loop where curl request is added.

$posting1 = "xmldata=" . rawurlencode($data);
$Medi_URL2 = "https://proda.mediemr.net:4443/pls/htmldb/MEDIDATA.GET_SURESCRIPT_DATA_NEW";
$i = 0;
$len = 32000;
$arr2 = array();

while($i < strlen($posting1)) {
    $arr2[] = substr($posting1, $i, $len);
    $i += $len;
}


for($cnt=0;$cnt<count($arr2);$cnt++)
{
      $value=      $arr2[$cnt];
        $ch = curl_init();
      
      curl_setopt($ch, CURLOPT_URL,$Medi_URL2);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS,$value);
   

 ### Return post result string, no just success code
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


  $result = curl_exec ($ch);
  curl_close ($ch);
   unset($ch);
  unset($value);
}
BadRequestError.jpg
vikasbapatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

F PCommented:
Try a foreach loop instead of a for loop which counts an array. It's faster and more flexible. Lemme know if this works for you:

foreach($arr2 as $value)
{
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL,$Medi_URL2);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS,$value);

 ### Return post result string, no just success code
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec ($ch);
    curl_close ($ch);
}

unset($value);

Open in new window

0
Ray PaseurCommented:
From the look of the BadRequestError, the problem may be in the information you're sending to the API.  You can get error messages from cURL, just in case the problem is in cURL and not in the data itself.  This script shows how to do that.

Just curious - why did you decide to break the data into chunks?  POST-method requests typically can handle large amounts of data.  Is there any API documentation that recommends breaking the data into chunks?

<?php // demo/curl_post_example.php

/**
 * Demonstrate how to use cURL to make a POST request
 * This may be used to start an asynchronous process
 *
 * http://php.net/manual/en/book.curl.php
 */
error_reporting(E_ALL);


function curl_post($url, $post_array=array(), $timeout=2, $error_report=TRUE)
{
    // PREPARE THE POST STRING
    $post_string = NULL;
    foreach ($post_array as $key => $val)
    {
        $post_string .= $key . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url           );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
    curl_setopt( $curl, CURLOPT_POST,           TRUE           );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
    curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"] = 'Ray';
$args["mail"] = 'Ray.Paseur@Gmail.com';

// ACTIVATE THIS TO SEE THE ARRAY OF ARGS
// var_dump($args);

// SET THE URL
$url = "http://Iconoun.com/demo/bounce_post.php";

// CALL CURL TO POST THE DATA
$htm = curl_post($url, $args, 3, TRUE);

// SHOW WHAT CAME BACK, IF ANYTHING
if ($htm)
{
    echo "<pre>";
    echo htmlentities($htm);
}
else
{
    echo "NO RESPONSE YET FROM $url -- MAYBE BECAUSE IT IS RUNNING ASYNCHRONOUSLY";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.