Jump to content

Curl Request Append Response


MoFish
Go to solution Solved by NotionCommotion,

Recommended Posts

Hello,

I am trying to do multiple curl requests and get the response for each one output to the screen.

I have wrote the following code, but only appear to be getting the last response when outputting $result.

What am i doing wrong? Is there a way to wait for each curl request to be completed before appending to $result so I can see them all?

Thanks very much

if (isset($_POST['submit'])) {
	$result = array();
	foreach ($textAr as $line) {
		$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 );
		curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
		$result[] = curl_exec ($ch);
	}
	curl_close ($ch);

	echo "<pre>";
	print_r($result);
	echo "</pre>";

}

 

Link to comment
Share on other sites

Hi,

It returns a string like the following:

string(506) " status error error The domain xxx.co.uk already exists in our database. This may occur if there is a pending Order for xxx.co.uk in our database under your account or any other account."

I would like to collate all these return strings for each of the curl request (could be 10+) and return them as an alert/echo at the end of them all.

if (isset($_POST['submit'])) {

	$result = array();

	foreach ($textAr as $line) {
		$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 );
		curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
		$result = curl_exec ($ch);
		curl_close ($ch);
		var_dump($result);
		
	}

	// i would like all of the strings to be compiled into one for returning to the screen
	// but it seems like it only gets the last one instead of them all, even if i try string appending.
	// is there a way to wait until one is completed before the next?

}

Thank you for your help.

Link to comment
Share on other sites

The first blob of code you posted was putting those results into an array and looks fine, but I can't help but notice that the request being made inside the loop is going to be the same every time. Shouldn't it be using $line somewhere?

Link to comment
Share on other sites

Hi @requinix

Apologies i removed that URL bit to simplify it. The $line is passed into the url of the curl call so each is unique.

When using the below code and calling 3 curl requests for example; it only gets the last string response even if i try to append.

I cannot figure out whats going on!

if (isset($_POST['submit'])) {

	$result = array();

	foreach ($textAr as $line) {
		
		$url = "https://xxx.com/api/domains/meh.xml?auth-userid=xxx&domain-name={$line}";

		$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 );
		curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
		$result = curl_exec ($ch);
        // $result[] = curl_exec ($ch);
		curl_close ($ch);
		print_r($result);
		
	}

    // print_r($result);

}

 

 

 

Edited by MoFish
Link to comment
Share on other sites

xxx1.com
xxx2.com

Array ( [0] => [1] => status error error The domain xxx2.com already exists in our database. This may occur if there is a pending Order for xxx2.com in our database under your account or any other account.)

As you can see, its only getting the result from the last one for some reason.

Link to comment
Share on other sites

@NotionCommotion

$line: www.xxx2.com $textAr: ["www.xxx1.com\r","www.xxx2.com"] Array ( [0] => [1] => status error error The domain xxx2.com already exists in our database. This may occur if there is a pending Order for xxx2.com in our database under your account or any other account. You may search for this domain within your control panel.)

 


	$result = array();
	
	foreach ($textAr as $line) {
				
		$url = "https://example.com/api/domains/blah.xml?domain-name={$line}";
		
		$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 );
		curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
		$result[] = curl_exec ($ch);
		curl_close ($ch);	
		
	}
	
	printf('$line: %s'.PHP_EOL, $line);
	printf('$textAr: %s'.PHP_EOL, json_encode($textAr)); 
	print_r($result);

 

Edited by MoFish
Link to comment
Share on other sites

I meant something like the following:

if (isset($_POST['submit'])) {

    printf('$textAr: %s'.PHP_EOL, json_encode($textAr)); 

    $result = array();
    
    foreach ($textAr as $line) {
                
        $url = "https://example.com/api/domains/blah.xml?domain-name={$line}";
        
        $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 );
        curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
        printf('$line: %s'.PHP_EOL, $line);
        $r = curl_exec ($ch);
        print_r($r);
        $result[] = $r;
        curl_close ($ch);    
        
    }
    
    print_r($result);

}

 

Link to comment
Share on other sites

$textAr: ["www.google.com\r","www.yahoo.com"]
$line: www.google.com
$line: www.yahoo.com status error error Domain yahoo.com already registered
Array ( [0] => [1] => status error error Domain yahoo.com already registered )

Its like the second request blanks out the first. If i do three requests, i only ever get the last one.

$textAr: ["www.google.com\r","www.yahoo.com\r","www.bbc.com"]
$line: www.google.com
$line: www.yahoo.com
$line: www.bbc.com status error error Domain bbc.com already registered
Array ( [0] => [1] => [2] => status error error Domain bbc.com already registered )

Edited by MoFish
Link to comment
Share on other sites

  • Solution

Okay, $textAr includes URLs for www.google.com\r and www.yahoo.com.  Do you really want the \r with Google?

Then I see $line: www.google.com being echoed, but don't see the results being echoed.  Replace print_r with var_dump as it likely is returning NULL.

Then I see $line: www.yahoo.com followed by the error message.  I would have expected a line break but not a big deal.

Then I see the $results array with the first element being empty and the second being the error response.

Isn't this what you were expecting?  Consider changing $result[] = $r; to $result[$line] = $r; to make it more clear.

Link to comment
Share on other sites

@NotionCommotion

I was hoping for something like this, as each of these are already registered.

Quote

Array ( [www.google.com ] => status error error Domain google.com already registered [www.bbc.com ] => status error error Domain bbc.com already registered [www.sky.com] => status error error Domain sky.com already registered )

However, only get the last response

Quote

$textAr: ["www.google.com\r","www.bbc.com\r","www.sky.com"]
$line: google.com
$line: bbc.com
$line: sky.com status error error Domain sky.com already registered
Array ( [www.google.com ] => [www.bbc.com ] => [www.sky.com] => status error error Domain sky.com already registered )

 

Link to comment
Share on other sites

@NotionCommotion

You are correct, some nulls are coming back but i don't understand why there would be no response message with them.

$textAr: ["www.google.com\r","www.bbc.com\r","www.sky.com"]
$line: google.com bool(false)
$line: bbc.com bool(false)
$line: sky.com string(203) " status error error Domain sky.com already registered "
array(3) { ["www.google.com "]=> bool(false) ["www.bbc.com "]=> bool(false) ["www.sky.com"]=> string(203) " status error error Domain sky.com already registered " }

Edited by MoFish
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.