cUrl not able to get the file from same server - php

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"; }

Related

Curl request not working to use hasoffers api

I am using curl request to hit the has-offers conversion url from my sevrver with the help of curl but it is not working.But when I call the same URL using a browser, it works.Is they can block CURL requests?.I am not getting why, is there any port blocking issue.
Below is php code to call url using curl request.
<?php
function curl_get_contents($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url="http://paravey.go2cloud.org/aff_l?offer_id=12&aff_id=1000";
$contents = curl_get_contents($url);
echo $contents;
?>
Please help me thanks in Advance
The url you are curling is a pixel tracking url:
http://paravey.go2cloud.org/aff_l?offer_id=12&aff_id=1000
The aff_l endpoint looks for a cookie with session information (hence why it works in the browser).
If you want to create conversions with server side code, you will need to store the session identifier (the transaction_id) in your system and use the aff_lsr endpoint to send that data to HasOffers to trigger a conversion.
The url for this would look like this:
http://paravey.go2cloud.org/aff_lsr?transaction_id= VALUE
Where Value is the session identifier you have stored.
I would ask the HasOffers support team if you have more issues with this.

php file with CURL commands not working on Server

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);
?>

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;

500 Server error file_get_contents

I have two project on same server. I want some data form on my website so I am using file_get_contents; most of the time I get the 500 internal error
I checked that my url fopen is on using phpinfo().
With default settings, file_get_content() doesn't work behind a proxy or it cannot handle timeouts. It's normally recommended to read local files.
Therefore use cURL instead.
Below function could be used for the job:
function http_request($uri, $time_out = 10, $headers = 0)
{
// Initializing
$ch = curl_init();
// Set URI
curl_setopt($ch, CURLOPT_URL, trim($uri));
curl_setopt($ch, CURLOPT_HEADER, $headers);
// 1 - if output is not needed on the browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Time-out in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
// Executing
$result = curl_exec($ch);
// Closing the channel
curl_close($ch);
return $result;
}
Let me know whether your're using Linux or Windows to give you cURL installation tips

curl CLI to curl PHP

I use the following command in some old scripts:
curl -Lk "https:www.example.com/stuff/api.php?"
I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.
What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.
I suspect something in the order of :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// What goes here so that I just get the Location and nothing else?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
The goal being $response = the data from the api “OK=1&ect”
Thank you
I'm a little confused by your comment:
// What goes here so that I just get the Location and nothing else?
Anyway, if you want to obtain the response body from the remote server, use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
If you want to get the headers in the response (i.e.: what your comment might be referring to):
curl_setopt($ch, CURLOPT_HEADER, 1);
If your problem is that there is a redirection between the initial call and the response, use:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Categories