Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

stream_context_create can be used with cURL?

Posted on 2009-05-12
11
Medium Priority
?
2,282 Views
Last Modified: 2012-05-06
Hi E's, the function stream_context_create can be used with cURL?

Regards, JC
0
Comment
Question by:Pedro Chagas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24370338
Hi E's, I made this question because I use the function stream_context_create:
================================
$url = "http://www.example.com/eeee/file.html";
$ctx = stream_context_create(array('http'=>array('timeout'=>1)));
$page = @file_get_contents($url, 0, $ctx);
if($page===false)
  echo 'not connect';
else
  echo 'connect';
===============================
Code above workly perfect
But if I use inside cURL like you can see in snippet code, the script don't work.

Maybe the problem not is the cURL, maybe is something wrong in my code.
//THE PAGE THAT CALL cURL
$URI = "http://www.example.com/tp_coneccao.php?url=$normurl&&palavra=$texto";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URI);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$apires = curl_exec($ch);
curl_close($ch);
 
//CODE EXECUTED IN cURL
include ("database.php");
$url_actual = $_GET['url']; 
$palavra_actual = $_GET['palavra']; 
$ctx = stream_context_create(array('http'=>array('timeout'=>1)));
$page = @file_get_contents($url_actual, 0, $ctx);
if ($page===false) { 
	mysql_query("update urls set chec=1, testeconecao=0 where urlhost='$url_actual' and palavra='$palavra_actual'", $db); 
	} else { //existe coneccao
	mysql_query("update urls set chec=1, testeconecao=1 where urlhost='$url_actual' and palavra='$palavra_actual'", $db); 
		}

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24373680
hi e's, maybe the question is confuse, i try simplify the question:
in snippet code you can see two scripts, in the first i have some urls from a database (like - http://www.example.com/file.html), i do a while to prepare all urls for the function in curl. that urls will be treatement in the second script, where i use 'stream_context_create' for check the connections of each url.
the problem is that don't work!!!

i don't know if the problem was run the function 'stream_context_create' in curl, or the problem is the 'while', maybe was the 'variables', or other thing. so, because i don't know where is the problem, i open this question.

regards, jc
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 1350 total points
ID: 24391294
stream_context_create is not related to curl. Curl has its own methods of setting timeouts. You can use curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24394373
hi @Roonaa, thanks for your feedback!
but where i substitute the code, where i put curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); in first or in second script?

regards, jc
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 24394409
In the first. You aren't using CURL in the second script as far as I can tell.
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24406915
Hello again, So, I have to substitute this line in my code?
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); for: curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);?

Regards, JC
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 24407019
No, you need them both.

-r-
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24407655
Hi, with the add of that line the script continue don't work!
The issue is maybe in other place.

Attention: The script work well without cURL. They work well if I execute everything in the same file, without 'curl_setopt'.
I don't do in the same file because I use cURL for take the data more fast.

Please help.

Regards, JC
//the file tp_relatorio.php contain now (with the new line of cURL):
$urlc_result = mysql_query("SELECT * FROM urls where palavra = '$palavradb'", $db);
$urlc_rows = mysql_num_rows($urlc_result);
while($urlc = mysql_fetch_object($urlc_result)) {
	$normurl = $urlc->urlhost;
	$URI = "http://www.example.com/tp_coneccao.php?url=$normurl&&palavra=$texto";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $URI);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); //the new line
		$apires = curl_exec($ch);
		curl_close($ch);
		//echo $apires;
		//echo "<br>\n";
 
//the file tp_coneccao.php contain
include ("database.php");
$url_actual = $_GET['url']; 
$palavra_actual = $_GET['palavra']; 
$ctx = stream_context_create(array('http'=>array('timeout'=>1)));
$page = @file_get_contents($url_actual, 0, $ctx);
if ($page===false) { 
        mysql_query("update urls set chec=1, testeconecao=0 where urlhost='$url_actual' and palavra='$palavra_actual'", $db); 
        } else { //existe coneccao
        mysql_query("update urls set chec=1, testeconecao=1 where urlhost='$url_actual' and palavra='$palavra_actual'", $db); 
                }
	}

Open in new window

0
 
LVL 49

Expert Comment

by:Roonaan
ID: 24409847
What actually is it you are trying to achieve?

As far as I can tell you are calling a particular script from domain B from a script on domain A. domain A script uses curl for better download performance, while the actual downloading of external content happens in the domain B script. As domain A script only triggers the domain B script, but does not process the results, what would be the benefit of using curl?
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24410118
@Roonaan, i use curl because i thing is more fast. everytime i call the script i have a lot of url's to check the connection, so i use curl to check all in simultain. maybe my idea about this is wrong, and i don't know if this scripts is the best solution for my goal.
please tell me something about this issue, if you have a great solution tell me.
what the script have to do is:
i have a lot of urls in db.
i call all results with a while.
in each while i go check if the connection is good for each url. (in this step i use the curl, but i'm not certain if it is the best solution).

regards, jc
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question