Search code examples
phpcurldrupal-7

Drupal 7 PHP cURL not executing


I'm attempting to send results from a drupal webform through a cURL POST to a third party. My cURL function is not working, and I'm struggling to find my error. I have never used cURL before, so I'm not really sure how it works, or really even what it does.

From what I can tell, I'm piecing together the URL to be sent correctly, the send is just failing.

<?php
module_load_include('inc','webform','includes/webform.submissions');
$uri = $_SERVER[REQUEST_URI];
$sid = substr($uri, 20);
$submission = webform_get_submissions(array('sid' => $sid));
$nid = $submission[$sid]->nid;

$sql = db_select('webform_submitted_data', 'w');
$sql->fields('w', array('sid','cid','data'))
    ->condition('sid', $sid)
    ->condition('cid', array(1,2,3,4,5,6,7,8),'IN');
$results = $sql->execute();

$post = NULL;
$url = urlencode('http://ulm.datamark.com/services/lead_submission&client_code=DAV4516&source_code=DAVNCU');

foreach($results as $result)
    {
        if ($result->cid == 1) {
            $post .= "first_name=" . urlencode($result->data);
        } else if ($result->cid == 2) {
            $post .= "&last_name=" . urlencode($result->data);
        } else if ($result->cid == 3) {
            $post .= "&email=" . urlencode($result->data);
        } else if ($result->cid == 4) {
            $post .= "&phone=" . urlencode($result->data);
        } else if ($result->cid == 5) {
            $who = $result->data;
        } else if ($result->cid == 6) {
            $post .= "&phone2=" . urlencode($result->data);
        } else if ($result->cid == 8) {
            $post .= "&comments=" . urlencode($result->data);
        }


    }
dsm($who);
dsm($url.$post);


if ($who == "fs")
{

 $defaults = array( 
        CURLOPT_POST => 1, 
        CURLOPT_HEADER => 0, 
        CURLOPT_URL => $url, 
        CURLOPT_FRESH_CONNECT => 1, 
        CURLOPT_RETURNTRANSFER => 1, 
        CURLOPT_FORBID_REUSE => 1, 
        CURLOPT_TIMEOUT => 4, 
        CURLOPT_POSTFIELDS => http_build_query($post) 
    ); 

   $ch = curl_init(); 
   curl_setopt_array($ch, ($defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        echo "Something went wrong";
        trigger_error(curl_error($ch)); 
    } 
    curl_close($ch);


?>

Solution

  • My eyes see a couple issues:

    1. Do not urlencode the URL itself. That function is for data you are passing in the Query portion of the url (the part after ?)
    2. It looks like you are missing the ? in your url.
    3. I think you have some logical issues in your assembly of the Post data. First, you are encoding all of $result->data for every post field. You likely want to include only one field at a time. Second, you are passing a string into http_build_query(), which expects either an array or an object. Perhaps you can refactor to use $post as an array, which might make it easier to debug. Read the docs for cURL and http_build_query to see what everything expects and does. For example, the cURL extension can do a lot of work for you if you pass an array into curl_setopt for CURLOPT_POSTFIELDS.