Retrieving RSS Feed from iTunes using cURL and PHP - php

Been trying to get some PHP cURL code to work that gets the RSS feed from iTunes when you give it the podcast URL. Here is the code:
$inputString = "curl -A 'iTunes/12.1.1.4 (Windows; U; Microsoft Windows 7 Home Premium Edition Service Pack 1 (Build 7601) DPI/96' -s 'https://itunes.apple.com/podcast/id530114975'";
$input = shell_exec($inputString);
$dom = new DOMDocument();
$html = $dom->loadHTML($input);
The cURL call when executed using shell_exec returns a blank string.
When I call the loadHTML function it gives the following error, which is pretty obvious given the cURL call doesn't return anything.....
Warning: DOMDocument::loadHTML(): Empty string supplied as input in C:\php scripts\itunesFeedExtractor.php on line 130
Now, I got the PHP cURL code from somewhere else, haven't used it before, and tried to modify it to match my computers setup.... I've changed Windows version, service pack, build no. (Don't know why the DPI/96 is needed so I left it alone)

You'd be better off using the PHP curl extension:
$ch = curl_init("https://itunes.apple.com/podcast/id530114975");
curl_setopt($ch, CURLOPT_USERAGENT, "iTunes/12.1.1.4 (Windows; U; Microsoft Windows 7 Home Premium Edition Service Pack 1 (Build 7601) DPI/96");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
But if you really want to use the shell_exec method, make sure curl is in your path - you can check by running the curl command from cmd / a terminal

Well I got it working by adding to more curl_setopt() options. The full code now reads:
$ch = curl_init("https://itunes.apple.com/podcast/id530114975");
curl_setopt($ch, CURLOPT_USERAGENT, "iTunes/12.1.1.4 (Windows; U; Microsoft Windows 7 Home Premium Edition Service Pack 1 (Build 7601) DPI/96");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
Cheers.....

Related

PHP cURL File Download over HTTP2

I have a php 7.4 script that downloads a zip file using cURL. Both servers are
Apache/2.4.51 (Fedora)
Fedora 35
OpenSSL version 1.1.11
If I use CURL_HTTP_VERSION_1_0 all works. CURL_HTTP_VERSION_2_0 does not. Apache on the server I am calling has protocol h2 set. Below are the pertinent lines of code.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // this is where I change to ver 2
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");
$html = curl_exec($ch);
the error I get using CURL_HTTP_VERSION_2_0 is Curl Error: transfer closed with 4 bytes remaining to read
Also, I can successfully cURL from the cli to the server from the same box the script is on with --http2.
What else should I try? Is there other info I should post to help answer?
EDIT: Is it possible the Content-Length header is being incorrectly set on the sending side?

Curl and file_get_contents timeouts in PHP (command line curl with same URL works normally)

I'm trying to retrieve the contents of a URL: https://www.cyber.gov.au/.
If I use wget or curl from the command line, all is fine. The response is almost instant.
$ wget https://www.cyber.gov.au/
--2020-11-17 08:47:12-- https://www.cyber.gov.au/
Resolving www.cyber.gov.au (www.cyber.gov.au)... 92.122.153.122, 92.122.153.201
Connecting to www.cyber.gov.au (www.cyber.gov.au)|92.122.153.122|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 41951 (41K) [text/html]
Saving to: ‘index.html’
index.html 100%[=========================================>] 40.97K --. KB/s in 0.002s
2020-11-17 08:47:13 (18.8 MB/s) - ‘index.html’ saved [41951/41951]
However, when I try to connect to the same URL through PHP curl, it times out with the message:
Operation timed out after 5001 milliseconds with 0 bytes received
I've reduced this to a test case:
$handle = curl_init('https://www.cyber.gov.au/');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 5);
curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36');
$output = curl_exec($handle);
echo $output;
curl_close($handle);
I also tried with various combinations of these additional curl settings, with no change:
curl_setopt($handle, CURLOPT_FRESH_CONNECT, true);
curl_setopt($handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // Also tried specifying v6
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0);
It doesn't seem to be the DNS resolution time:
echo curl_getinfo($handle, CURLINFO_NAMELOOKUP_TIME); // 0.012 seconds
I've tried this on different machines, with different versions of PHP (7.2.12 and 7.4.10), and I get the same behaviour. Other URLs, both HTTP and HTTPS, work as expected. I get the same on CLI PHP as through Apache. Trying file_get_contents() gives a similar result, it just times out. Adding verbose curl logging didn't provide any more information.
curl --version gives curl 7.47.0 and curl 7.58.0 on the machines I tested on.
Can anyone spot what's going on or point me in the right direction to find out more about the problem?

Curl: Showing "Can't resolve domain name" error of the all localhost sites from PHP

Today my cURL is starting to show me this error Could not resolve: {local-domain.test} (domain name not found)
All working fine if I'm trying to get some info from an external link, but local sites aren't working properly.
So, here is my code
public function send($type, $url, $data)
{
$type = strtolower($type);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($type == 'post') {
$urlWithData = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlWithData);
}
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$out = new \stdClass();
$out->status = $status;
$out->body = json_decode($result, true);
return $out;
}
All start to work if I will add that host into my /etc/hosts/ file but I have a huge list of the domain and before updating PHP
all worked fine on my side. I'm using dnsmasq as a dynamic host resolving tool.
OS: MacOs High Siera
Apache: 2.4.37
PHP: Multiple PHP versions, from 5.6-7.3
Some debug info:
Ping to that domain from the terminal is working fine (showing localhost)
Everything is working fine even If I run curl -v local-domain.test from a terminal
I've tried to use CURLOPT_DNS_USE_GLOBAL_CACHEoption.
Cleared all DNS cache of my machine from a terminal
Maybe I forgot to add something to the php.ini of each PHP? Also, all worked fine before I've made updating process of them all.
This answer on this thread worked for me.
Running:
brew uninstall curl-openssl --ignore-dependencies
(removes the last version of curl-openssl, ensuring parity between curl versions used by your system and PHP)
... then followed by a valet restart
... allowed local curl to resolve Valet hostnames properly.

MAMP php 5.6.2 file_get_contents not working (timeout)

I have searched and cannot find a reason why this isn't working. I have tried executing from command line as well as apache and it does not work.
<?php
echo file_get_contents("http://google.com");
?>
I have a simple php script as shown above that simply makes a request to google and outputs the contents.
Error:
Warning: file_get_contents(http://google.com): failed to open stream: Operation timed out in /Applications/MAMP/htdocs/p.php on line 2
PHP version:
PHP 5.6.2 (cli) (built: Oct 20 2014 16:21:27)
allow_url_fopen = On
The odd part is if I use /usr/bin/php it works fine. (Built in apple version)
Here is the wireshark capture:
https://www.dropbox.com/s/u7jdg494fmud22i/packet_capture_php_network.pcapng?dl=0
I suggest you to use a CURL alternative:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Your apache server might have restrictions on it. Check out this post which details the fix. Should just be a config change in your php.ini file (make sure you change the MAMP PHP's php.ini file).

php use of file_get_contents with https -

I looked at the threads and followed suggestions - which got me here...
I'm using WAMP - php rev 5.4.12 (Win7)
Code is as simple as possible:
$result = file_get_contents("https://g4apps.bliptrack.net/blipzones/report/publicdisplayapi.seam?display_id=dvp_vms4");
(this URL returns an XML file - works in browsers....)
The error is
"Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?..."
I did add to php.ini:
allow_url_include = On
extension=php_openssl.dll
to no avail
When I ask, I get: openssl: no http wrapper: yes https wrapper: no
Any suggestions? (I work w/PHP but not an expert...)
You should use cURL here instead of socket connection.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://g4apps.bliptrack.net/blipzones/report/publicdisplayapi.seam?display_id=dvp_vms4");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlPage = curl_exec($ch);
curl_close($ch);
notice use of "CURLOPT_SSL_VERIFYPEER" as 0/false.

Categories