Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save cURL Display Output String in Variable PHP

is their an option to save the outpout of a curl request in a php variable?

Because if i only save the $result i get a 1 or nothing

<?php
$url='http://icanhazip.com';
$proxy=file ('proxy.txt');
$useragent='Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';

for($x=0;$x<count($proxy);$x++)
{
$ch = curl_init();
//you might need to set some cookie details up (depending on the site)
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_URL,$url); //set the url we want to use
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, $proxy[$x]);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); //set our user agent
$result= curl_exec ($ch); //execute and get the results
print $result; //display the reuslt
$datenbank = "proxy_work.txt"; 
$datei = fopen($datenbank,"a");
fwrite($datei, $result);  
fwrite ($datei,"\r\n"); 
curl_close ($ch);
}
?>
like image 678
The Masta Avatar asked Aug 13 '13 07:08

The Masta


People also ask

How can get cURL value in PHP?

php $url = 'hxxp://domain.com/univ/v8?q=tas+wanita'; $ch=curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $r=curl_exec($ch); curl_close($ch); print_r(json_decode($r, true)); ?>

What is the use of Curl_init in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.

How get cURL URL in PHP?

PHP $url = "http://www.ecs.soton.ac.uk/news/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $html = curl_exec($ch); $status_code = curl_getinfo($ch,CURLINFO_HTTP_CODE); if($status_code=302 or $status_code=301){ $url = ""; // I want to to get the destination url } ...

How to return curl output as a string in PHP?

This is a short PHP tutorial on how to return cURL output as a string. As you probably already know, the default behavior of cURL is to simply print the response out onto the page. However, there is a quick fix for this. The CURLOPT_RETURNTRANSFER option. The curl_setopt function allows you to configure various options for a cURL request.

How to save the outpout of a curl request in a variable?

is their an option to save the outpout of a curl request in a php variable? Search "CURLOPT_RETURNTRANSFER" in here. You need to set CURLOPT_RETURNTRANSFER option to true. With this you can avoid the output and make the programme continue running. Not the answer you're looking for?

How to call a function from a variable in PHP?

One is directly calling function by variable name using bracket and parameters and the other is by using call_user_func () Function but in both method variable name is to be used. echo $msg." "; Another Method: Using eval () Function: The eval () function is an inbuilt function in PHP which is used to evaluate string as PHP code.

Is it possible to curl data into a shell script variable?

I was hoping to curl data into a shell script variable, then use cat or awk to operate on that variable as if it were a file. Is this possible, or is there a workaround? Sure you can.


2 Answers

You need to set CURLOPT_RETURNTRANSFER option to true.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
like image 118
poncha Avatar answered Sep 23 '22 16:09

poncha


You need to add a setting of curl option CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

With this you can avoid the output and make the programme continue running.

like image 41
如果有时间 Avatar answered Sep 23 '22 16:09

如果有时间