I have many HTTPS-URLS where I have to find a Special Phrase, so I´m trying to use cURL in a foreach Loop, but it´s not working.
...
foreach($sites as $site) {
$URL = $site;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
}
print substr($response, $start, $i);
...
If I use just a single HTTPS-URL I get the Phrase, but inside a foreach-loop it´s not working.
Can someone help me? (:
Please excuse my poor english..
this may help :) store result inside an array
$sites = ['https://stackoverflow.com','http://example.org'];
$result = [];
foreach ($sites as $site) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $site);
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Curl");
// 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);
// execute the given URL, and return output
$result[] = curl_exec($ch);
curl_close($ch);
}
var_dump($result);
Related
I have a variable $arr in PHP. It has the following data inside it.
This is my PHP Code.
$data = array(
'Request' => 'StockStatus',
'merchant_id' => 'shipm8',
'hash' => '09335f393d4155d9334ed61385712999'
);
$url = 'https://ship2you.com/ship2you/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$arr = json_decode($result, true);
foreach ($arr as $value) {
echo $value['packagename'];
}
I want to loop through it. How can I achieve it? I tried using foreach but it gives me error. Thanks in advance.
You have to decode your CURL output string twice:-
$arr = json_decode(json_decode($result, true),true);
foreach ($arr as $value) {
echo "<pre/>";print_r($value['packagename']);
}
Note:- #Xatenev mentioned the correct thing:-
The json has escaped quotes. When a json with escaped quotes is passed to json_decode() it only removes all the escaped sequences. When calling json_decode() again, it decodes it correctly
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$arr = json_decode($result);
foreach ($arr as $arr_item) {
echo $arr_item->user_id;
}
json_decode($json, true);
If the second parameter is true, it will return array. In case it is not what you are looking for, please show the code you have.
I have a curl call I want to run but once it runs, I would like to run another to a different url but with the same options.
Can I run another call without having to copy and paste the options, I need to run about 5 calls, it seems like there is a way to accomplish this. I cannot run them all at the same time, I need to make sure that I get the result from one, then if certain critera is met, I need to run another one.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
curl_close($ch);
Simply update the url (using the CURLOPT_URL option) before each additional request. See the comments in the below example.
// initialize with the first url you want to use
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
// check the result of the first request
if($result == "the content you want")
{
// if the result dictates that you make another request, update the url
curl_setopt($ch, CURLOPT_URL, $url2);
// execute the second request
$result2 = curl_exec($ch);
// do something with $result2
}
// only close curl after you are done making your requests
curl_close($ch);
If I haven't understood wrongly.
Yes, I always use a way to run 1 cURL action to more than 1 URL.
And you should use:
EDIT: Method 2 isn't working.
Method 1:
<?php
// Arrays
$ch=array();
$url=array();
$result=array();
// ************
$url['1'] = 'http://url1.com';
$url['2'] = 'http://url2.com';
$ch['1'] = curl_init($url['1']);
curl_setopt($ch['1'], CURLOPT_HEADER, true);
curl_setopt($ch['1'], CURLOPT_NOBODY, true);
curl_setopt($ch['1'], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch['1'], CURLOPT_TIMEOUT, 10);
$result['1'] = curl_exec($ch['1']);
curl_close($ch['1']);
$ch['2'] = curl_init($url['2']);
curl_setopt($ch['2'], CURLOPT_HEADER, true);
curl_setopt($ch['2'], CURLOPT_NOBODY, true);
curl_setopt($ch['2'], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch['2'], CURLOPT_TIMEOUT, 10);
$result['2'] = curl_exec($ch['2']);
curl_close($ch['2'])
?>
Method 2
<?php
$url = array(
'http://url1.com',
'http://url2.com'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
curl_close($ch)
?>
In method 1 we used arrays in $ch and $url and $result
In method 2 we made array of $url
I am trying to use this PHP script to get the shortened link from bit.ly API.. It works fine but my question is there any way to make this script more efficient or take some unnecessary parts out of it. Also my main question is that when I use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
I have to use the trim function on $data but when I use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
I don't have to do that.. Why is it causing a line break after the link when I use 1 instead of true?
<?php
function get_bitly_short_url($url, $format = 'txt')
{
$connectURL = 'http://api.j.mp/v3/shorten?login=(MY USERNAME)&apiKey=(MY API)&uri=' . urlencode($url) . '&format=' . $format;
return curl_get_result($connectURL);
}
function curl_get_result($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return trim($data);
}
$short_url = get_bitly_short_url('http://google.com');
?>
1 !== true in common case, but in your case, should be no difference,
double check all other things...
I make simple test for you:
<?php
$url = 'http://ziptasticapi.com/92530';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data1 = curl_exec($ch);
curl_close($ch);
var_dump($data);
echo "\n";
var_dump($data1);
Results:
string(52) "{"country":"US","state":"CA","city":"LAKE ELSINORE"}"
string(52) "{"country":"US","state":"CA","city":"LAKE ELSINORE"}"
So, bugs in php happens, but no bugs this time
No difference, check other parts of your system
Luck!
Below I have a piece of code to return some information through an API.
$page = 'http://api.duedil.com/sandbox/v2/company/03977902.json? fields=get_all&api_key=***********';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
This prints out the following results http://www.visitrack.co.uk/testdata.php
what i am trying to do is split the results in to individual variables.
Thanks
Something like this should do the trick:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // forces curl_exec() to return page as a string
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, TRUE); // parse json string to array
print_r($data);
I have a problem with cURL. It takes over 40 seconds to fetch a web page.
The function is:
function get_page(){
$url = get_url();
$timeout = 1000;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, $CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$return_data = curl_exec($ch);
print_r (curl_getinfo($ch));
curl_close($ch);
return $return_data;
}
Also, it seems that $return_data = curl_exec($ch) actually dumps the page.
I managed to solve this problem by changing the DNS to 8.8.8.8