0

I'm trying to run a CURL command in my PHP file, but I'm not able to see the output. The following is modified slightly without the real usernames/passwords/URLs. The reason I'm trying to see the output is to make sure the CURL command is working as expected (I've run it in bash so I know the expected result).

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0');
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: text/plain;charset=utf-8';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, '$username:$password');
curl_setopt($ch, CURLOPT_POSTFIELDS, '$data');

// grab URL and pass it to the browser
curl_exec($ch);

I've run the following to make sure CURL is installed with my PHP server, and that is the case:

function _is_curl_installed() {
    if  (in_array  ('curl', get_loaded_extensions())) {
        return true;
    }
    else {
        return false;
    }
}

// Ouput text to user based on test
if (_is_curl_installed()) {
  echo "cURL is <span style=\"color:blue\">installed</span> on this server";
} else {
  echo "cURL is NOT <span style=\"color:red\">installed</span> on this server";
}

Is there something I'm doing wrong? I've run the curl command on my bash, and I'm able to see a response fine:

curl -X POST --user $username:$password --header "Content-Type: text/plain;charset=utf-8" --header "Accept: application/json" --data-binary @Test.txt "http://www.example.com"
Adam
  • 2,384
  • 7
  • 29
  • 66

1 Answers1

1

Try this example:

<?php
// SEND CURL POST DATA
 $jsonData = array(
    'user' => 'Username',
    'pass' => 'Password'
); 
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//API Url
$url = 'http://fxstar.eu/JSON/api.php';
 //Initiate cURL.
$ch = curl_init($url);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1); 
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); 
//Set the content type to application/json
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(Content-Type: text/plain;charset=utf-8); 
//Execute the request
echo $result = curl_exec($ch);
?>

Get json from js:

<?php
// GET JSON CONTENT FROM JS api.php
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
$json = json_decode($jsonStr);
?>
  • Hasn't worked for me. Is there some sort of fiddle example? – Adam Feb 24 '17 at 19:01
  • http://stackoverflow.com/questions/31970541/php-curl-how-to-set-body-to-binary-data –  Feb 25 '17 at 08:03
  • Tutorial http://ryansechrest.com/2012/07/send-and-receive-binary-files-using-php-and-curl/ –  Feb 25 '17 at 08:03
  • Working curl post, post json, get methods: https://github.com/fxstar/openshift/blob/master/curl/curl-post-get-post-json.php –  Mar 26 '17 at 11:21