0

I know how to do this using httpRequest:

    //create the httprequest object                
    $httpRequest_OBJ = new httpRequest($url, HTTP_METH_GET, $options);

    //add the content type
    $httpRequest_OBJ->setContentType = 'Content-Type: application/Json; charset=utf-8';
    //add the raw post data
    $httpRequest_OBJ->setRawPostData ($theData);
    //send the http request
    $result = $httpRequest_OBJ->send();

    try{
    $response = $result->getBody();
    }catch (HttpException $ex){

        echo $ex;


    }

What is the cURL equivalent?

Cheers.

3 Answers 3

1

You can use cURL by following way

$ch = curl_init($url); // Put your URL here
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if($ispost==1){ // IF it is a post request
   curl_setopt($ch, CURLOPT_POST, 1);
}
else{
   curl_setopt($ch, CURLOPT_POST, 0);
}
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$output = curl_exec($ch);
curl_close($ch);        
print_r($output);
Sign up to request clarification or add additional context in comments.

2 Comments

curl_setopt($ch, CURLOPT_POST, 0); so that means GET?, I find it odd that you cant write GET explicitly
That's right friend. for GET request it is 0 and 1 for POST. Actually by default it is GET.
1

Something like this should work.

function apiCall($type, $params=array())
{
    //building up the url parameters
    $args=array();
    foreach($params as $field=>$value)
    {
        // Seperate each column out with it's corresponding value
        $args[]=$field.'='.str_replace(' ', '+',$value).'';
    }
    // Create the query
    $url = $this->baseurl.$type.'?'.implode('&',$args);
    //check if CURL is available, if so use curl to make the call, otherwise get the file content via file_get_contents()
    if (function_exists('curl_init'))
    {
        $ch = curl_init();
        // Set the URL
        curl_setopt($ch, CURLOPT_URL, $url);
        // Removes the headers from the output
        curl_setopt($ch, CURLOPT_HEADER, 0);
        // Return the output instead of displaying it directly
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0');
        // Execute the curl session
        $result = curl_exec($ch);
        $errno = curl_errno($ch);
        // CURLE_SSL_CACERT || CURLE_SSL_CACERT_BADFILE
        if ($errno == 60 || $errno == 77) {
        self::errorLog('Invalid or no certificate authority found, '.
            'using bundled information');
        curl_setopt($ch, CURLOPT_CAINFO,
            dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ca_chain_bundle.crt');
        $result = curl_exec($ch);
        }
        // Close the curl session
        curl_close($ch);
        $res = json_decode($result, true);
        return $result;

    }
    else
    {
        // fallback if we don't have CURL or if ifs curently not available (whysoever)
        // A litte slower, but (usually) gets the job done
        $res =  file_get_contents($url);
        return json_decode($res, true);
    }
}

Usage would be something similar to this:

$res = $this->apiCall('search', array('term'=>$term, 'entity'=>$entity, 'country'=>$country));

This returns an associative array.

2 Comments

Woa... seems like an over kill. The OP is trying to make a simple GET request.
well yeah its from my current project where I have to parse the result of the apple search / lookup api and the amazon affilate api.
1

Take a look at this:

http://www.php.net/manual/en/curl.examples.php

Example:

<?php

// helper function
function curlGet($url)
{
    // create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, $url);

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

    // finished
    return $output;
}

// usage
var_dump(curlGet('http://google.com/'))

If the server you are making the GET request to is responding with JSON string, you can decode it like this:

$jsonStr = curlGet('http://pastebin.com/raw.php?i=S62Ar7mY');
$jsonObj = json_decode($jsonStr);

echo "<pre>";
print_r($jsonObj);

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.