php file with CURL commands not working on Server - php

I have php file which consist of curl code so as to access an api. But when I upload this file on server and try to execute it via url no output is shown. Also file symbol is not shown as it is shown for php file. Guide me how to solve this problem. The code in php file is :
<?php // set up the curl resource
$ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch,CURLOPT_POSTFIELDS,"{}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, 'http://184.73.165.170:3000/api/3ace632b194e4366b821f7775b51cc4e/get-ibeacon-contents'); // execute the request
$output = curl_exec($ch);
// output the profile information - includes the header
//echo "curl response is :".($output). PHP_EOL; print($output);
// close curl resource to free up system resources
curl_close($ch); ?>
My server index shows as shown in image. As per my understanding a php file symbol is different than what it is shown in image I uploaded. Can anyone suggest about this ,if it helps me resolve my aim.

I tried on local and the cURL seems to work. If its working on local only and not on server then these are the things you should check:
Enable cURL on server ;extension=php_curl.dll and remove the semicolon; (uncomments) usually inside file php/php.ini
If you are working locally on Windows and trying to upload to a Linux server, then you might want to change EOL for UNIX/OSX Format. You can do this using Notepad++ by going to Edit -> EOL Conversion -> UNIX/OSX Format and then upload.

Try using your code this way
<?php
$json=json_encode(array('key1'=>'val1','key2'=>'val2'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://184.73.165.170:3000/api/3ace632b194e4366b821f7775b51cc4e/get-ibeacon-contents');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$json);
$output = curl_exec($ch);
var_dump(curl_getinfo($ch));
echo curl_errno($ch) . '-' . curl_error($ch);
curl_close($ch);
?>

Related

CURL in php returns a display text in my page

Hi i have this curl to download link files from the sendthisfile.com. Now my problem is when I try to test it last week my code runs well. But now when I test it this is what I got to my web app. I suspect this is from the curl when processing the files. Can someone help me figured this thing out? this is my code below in curl
$zipUrl = "https://download.com/sample.zip";
$fileName = date().".zip"; //create a random name or certain kind of name here
$fh = fopen($filename, 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $zipUrl);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
curl_exec($ch);
curl_close($ch);
can someone help me why this throws a texts in a webpage?
You're missing this:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
This tells curl to return the output instead of directly displaying it. It's also noteworthy that you should set CURLOPT_RETURNTRANSFER before CURLOPT_FILE as I've read here.
See the official documentation for more information.

Read response from server which downloads as a file

I am trying to read the response (json text) from server. But the server returns the response as a file which gets downloaded in my downloads directory.
url:-https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22id%22%3Anull%2C%22name%22%3Anull%2C%22type%22%3A%22%5C%2Fastronomy%5C%2Fplanet%22%7D%5D
I am using curl in my php code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Connection: Keep-Alive'
));
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
How can I read the data using php curl?
UPDATE: When I try to run the same code in online editors like http://phpassist.com/ then it reads the data and shows me the required output.
So is there any additional configuration I need to make in XAMPP??
Tks
You need disable ssl certification or get ssl certification, the fastest way is:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

cUrl not able to get the file from same server

I am trying to fetch file from the same server i am running my php script in which i am cUrl to fetch it.
It does not download file and get timeout.
I am able to get the file using same url from browser.
cUrl is able to get the file if the url is anything other than the same server.
Are their any settings i need to modify to support file download using cUrl on same server.
Appreciate your help here.
My code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $file_url);
$content = curl_exec($ch);
curl_close($ch);
try this with all parameters of curl
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
//curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
Use Google Chrome' Copy as cURL (open "dev tools", "Network" tab, right-click on a request) on a successfully fetched file, paste to a terminal, then execute.
If everything is fine, and you are able to get a file via curl at terminal, then there is something with headers and/or request params. If not, it's more likely that something wrong with your network configuration.
Also, check web server logs, do you even get to a web server with your script?
Are you using 127.0.0.1 or localhost or full TLD? Is browser connecting to Internet via proxy?
The workaround I found is this:
Test if you are on your own server, then use a simple include for the same script:
if (($ser = servername()) != $floc) {
return(GetFileContent("$floc/$page.php"));
} else { include "$page.php"; }

Response received from a url via php script is smaller than when I open it in browser

I'm trying to fetch this url via a script: http://api.alarabiya.net/sections/2/
But JSON response received is much smaller than when I open it directly in a browser,
please notice that I tried this url through CURL and set the same USER-AGENT of the browser and all request header used in the browser and I still get a smaller response.
Here's an exmaple using just file_get_contents
<?php
echo file_get_contents("http://api.alarabiya.net/sections/2/");
?>
My question is if there's a request size limit when using file_get_contents or if the PHP's memory can't handle it or what's the problem exactly?
When I CURLed this in shell it gave me the same o/p as in php (the trimmed output).
I finally found a solution for this:
$url = "http://api.alarabiya.net/sections/2/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
// This is what solved the issue (Accepting gzip encoding)
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$response = curl_exec($ch);
curl_close($ch);
echo $response;

Using PHP CURL to download a file

I am trying to download a .exe file using PHP CURL from the web server to the PC, using CURL to make the webserver login automatic. The problem I'm having is that the file is being written to the browser window instead of asking the user to save or run the file. I'm using the code below, how can I fix this to make it work like I want?
$username = "myuser";
$password = "mypassword";
$url="http://www.mywebsite.com/files/softwaredownload/myfile.exe";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_exec ($ch);
curl_close($ch);
You should set the headers so the browser knows it has to display the save dialog.
Content-Disposition: attachment; filename=<file name.ext>
Also, bear in mind that Content-Type header should be before Content-Disposition.
Add to your code these lines:
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
... and replace curl_exec($ch) with $response = curl_exec($ch);, so $response content might be saved into a local file with file_put_contents() of fwrite() methods.

Categories